gpt4 book ai didi

debugging - 数组为空时如何触发中断?

转载 作者:行者123 更新时间:2023-11-28 05:31:01 24 4
gpt4 key购买 nike

在我的应用程序中,当我尝试访问数组中的索引时遇到问题,该数组实际上是空的。我找不到清空它的内容,所以我想知道调试器是否可以创建一个动态断点,当我的数组为空时该断点会弹出。因此,只要有东西重置数组或拿走它的最后一个对象,我就想知道。

我尝试创建一个以“myArray.isEmpty == true”为条件的符号断点,但它看起来不像我想要的那样工作。

这是可能的还是我只是在做梦?

谢谢

最佳答案

正如@kendall 提到的,您可以使用didSet 来检测数组何时被清空,并在其上放置一个断点:

// 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())

关于debugging - 数组为空时如何触发中断?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28447687/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com