简体   繁体   English

Swift iOS。 当我尝试更改按钮数组时,程序崩溃

[英]Swift iOS. Program crashes when i try to mutate an array of buttons

I have about 20 buttons linked to allKeys . 我有大约20个链接到allKeys按钮。 I want the turnRed button to change the color of their text to red. 我希望turnRed按钮将其文本的颜色更改为红色。 I tried the following code: 我尝试了以下代码:

    @IBOutlet var allKeys: [UIButton]!


    @IBAction func turnRed(sender: UIButton) {
        var i = allKeys.count

        repeat {
            allKeys[i].setTitleColor(UIColor.redColor(), forState: UIControlState.Normal)
        i -= 1
        } while i != -1
    }

When I press the turnRed button, my program crashes and xcode jumps to the appDelegate file and highlights the AppDelegate class with the error Thread1: signal SIGABRT. 当我按下turnRed按钮时,程序崩溃,xcode跳至appDelegate文件,并突出显示AppDelegate类,错误为Thread1:signal SIGABRT。

The console says "Terminating app due to uncaught exception 'NSRangeException Reason: NSArrayI objectAtIndex 控制台显示“由于未捕获的异常'NSRangeException而终止应用程序,原因:NSArrayI objectAtIndex

The issue here is that you are setting the variable i to allKeys.count , but since arrays in swift start with zero, the twentieth element should actually be allKeys[19] . 这里的问题是,您将变量i设置为allKeys.count ,但是由于swift中的数组从零开始,因此第二十个元素实际上应该是allKeys[19] Thus, when the loop is at i = 20 , it tries to access allKeys[20] , which crashes the app. 因此,当循环在i = 20 ,它将尝试访问allKeys[20] ,这会使应用程序崩溃。

One way to solve this is to set i to allKeys.count - 1 instead, but a better way would be to use a for-in loop instead of repeat . 解决此问题的一种方法是将i设置为allKeys.count - 1 ,但是更好的方法是使用for-in循环而不是repeat

for key in allKeys {
    key.setTitleColor(UIColor.redColor(), forState: UIControlState.Normal)
}

There are multiple ways to loop in Swift, and you should use the options to your advantage. 在Swift中有多种循环方法,您应该使用这些选项以发挥自己的优势。 You can find Apple's documentation for Control Flow here . 您可以在此处找到Apple的Control Flow文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当我尝试对指针数组中的数组进行free()时,程序在特定值上崩溃 - Program crashes on a specific value when i try to free() the arrays in a pointer array 谁能告诉我为什么当我尝试在菜单驱动的数组操作程序中调用 Insert 或 Delete 函数时我的程序会崩溃? - Can anyone tell me why my program crashes when I try to call the Insert or Delete function in a menu driven array manipulation program? 当我尝试访问多维数组时,应用程序崩溃 - Application crashes when I try to access a multidimensional array Visual Studio C ++ 每当我尝试从[80] x [80]数组中循环一个cout时,程序就会崩溃 - Visual Studio C++ | Program crashes everytime I try to loop a cout from an array of [80]x[80] 突变字符串的迅捷数组(Swift 2) - Mutate swift array of strings (swift 2) 当我将值推入数组并尝试重新打印值时,页面崩溃 - My page crashes when i push a value into an array and try to reprint the values 当我尝试从对象数组调用方法时,我的 Java 代码崩溃 - My Java code crashes when I try to call a method from an array of objects 将对象添加到数组时程序崩溃 - Program crashes when adding object to array 迭代和变异字典数组Swift 3 - Iterate and mutate an Array of Dictionaries Swift 3 实体数组不会迅速变异吗? - Array of entity does not mutate in swift?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM