数组为空时如何触发中断? [英] How to trigger a break when an Array is empty?

查看:75
本文介绍了数组为空时如何触发中断?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

我试图用 myArray创建一个符号断点。 isEmpty == true作为条件,但它并不能按照我想要的方式工作。

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?

谢谢

推荐答案

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

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

这篇关于数组为空时如何触发中断?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆