How to trigger a break when an Array is empty?

删除回忆录丶 提交于 2019-12-13 18:18:20

问题


In my app, I have a problème when I try to reach an index in an Array, the Array is actually empty. I cannot find what is emptying it so I was wondering if it's possible with the debugger to create a dynamic breakpoint that will pop up when my array is empty. So as soon as something is either resetting the array or taking away its last object, I'd like to know.

I tried to create a symbolic breakpoint with "myArray.isEmpty == true" as the condition but it doesn't look to work the way I want.

Is it possible or I'm just dreaming?

Thanks


回答1:


As @kendall mentions, you could use didSet to detect when the array is being emptied, and put a breakpoint on it:

// a acts like a normal variable
var a: [Int] = [] {
    // but whenever it’s updated, the following runs:
    didSet {
        if a.isEmpty {
            // put a breakpoint on the next line:
            println("Array is empty")
        }
    }
}

a.append(1)
a.append(2)
println(a.removeLast())
// will print “Array is empty” before the value is printed:
println(a.removeLast())  



回答2:


What you want is called a Watchpoint, which lets you monitor changes in memory. I'm not sure yet how to set one on a Swift Array, but that could be a good starting point for research.

One idea would be to add a didSet{} block to the property that holds the array, adding a log statement within - break on that based on your condition that the array is empty.




回答3:


To the best of my knowledge this isn't possible with Swift and Xcode (or with any other language of IDE I have used). To make this work the IDE would have to continually evaluate the given expression at every step of programs execution.

Now, if arrays were classes, you could subclass and add a breakpoint in an override isEmpty method, but as they are classed you cannot. :-(



来源:https://stackoverflow.com/questions/28447687/how-to-trigger-a-break-when-an-array-is-empty

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!