2
func buttonAppearance() {
    let buttons = [buttonOneView, buttonTwoView, buttonThreeView, buttonFourView, buttonFiveView, buttonSixView, buttonSevenView, buttonEightView, buttonNineView, buttonTenView, buttonElevenView, buttonTwelveView, previousButtonView, skipButtonView, submitButtonView]
    for button in buttons {
        button?.layer.cornerRadius = 5.0
        button?.layer.shadowOffset = CGSize(width: 2.0, height: 2.0)
        button?.layer.shadowColor = UIColor.red.cgColor
        button?.layer.shadowOpacity = 1.0

    }
}

Whenever I comment out the array and any code related to it, the indexing completes and I can run and build just fine. Has anyone else had any issues with this? I researched and saw that it was a bug fixed with Xcode 8.1, but I am all up to date and it's still an issue. I imagine there is a workaround, but that is just dirty.

hmiranda3
  • 43
  • 5
  • 2
    Try giving it an explicit type annotation – see for example http://stackoverflow.com/questions/28411298/swift-array-causing-indexing-issues-in-xcode-6-1-1. If that doesn't work, then get rid of the array literal altogether and append each element, see http://stackoverflow.com/questions/26494082/swift-xcode-index-freezing-or-slow – Hamish Nov 07 '16 at 22:19
  • It seems your are going to provide same appearance to all button, so better you create one custom class which is subclass of UIButton and add above properties in init() method. Then go to storyboard and assign this custom class as button's class. – Karthick Selvaraj Nov 08 '16 at 08:15
  • Thank guys! Explicit declaration seemed to do the trick! Although, you are right, Karthick, I will definitely make a custom class. That would clean up the code significantly! Thanks again! – hmiranda3 Nov 08 '16 at 18:58

2 Answers2

3

remove the optional question mark after button. In your array you have as many items as the loop will go through. so you know that "button" will exist in your array as an iterator since you have items in it and you are looping through it.

You say that if you comment out the array the indexing works. But your for loop inside the buttonAppearance() function is DIRECTLY related to the array, so I am not sure what exactly gets indexed when you comment out the array and its related code.

An important question to ask yourself is the data types of items that you placed in the array and whether you need to conform to some protocol if you extended any of your button objects inside of the array. Surely the for loop would work on primitive data types, so if its not an issue with optional (implicit/explicit) handling - then please check your types for the objects you placed inside that array.

Another thing to consider - are you trying to create your buttons array every time the function is called (like changing the appearance theme) or are you trying to reference some global buttons which already exist? In that case - declare the button array outside of the function and just loop through the array inside the function.

If on the other hand - you intend to create the buttons ONLY when the function is called and they dont exist outside of the function globally, you would provide more info about the objects and their type.

It seems to me you have the buttons defined globally outside the function and you are trying to store them all in an array just to loop through them - in which case I would suggest removing the optional "?"

Store the buttons in the array - outside of the function and loop through the array in the function. hope that helps.

  • What I meant by commenting the array was really commenting out the body of the function (the rest of the function wouldn't work without the array). So basically when I would have an empty function, the indexing issue would resolve. The buttons were indeed a set that were defined globally and I was placing them in the array when the function was called. It seems, however, that simply explicitly declaring the array type also resolved the indexing issue. That said, your advice made me change a few things that helped make my code a little clearer! Thanks a ton for helping this novice! – hmiranda3 Nov 08 '16 at 19:04
2

Try to explicitly declare array type like this:

 let buttons: [UIButton] = [buttonOneView, buttonTwoView, buttonThreeView, buttonFourView ... ]

It can be an issue of a Swift type infer system.

Zapko
  • 2,421
  • 23
  • 30