NSArray element failed to match the Swift Array element type

Hey guys, I am doing the App Development with Swift book and have reached the "Apple Pie" project. Everything was going fine until the very last function "enableLetterButtons(_ enable: Bool)" when I try to run the app with this code I get only a white screen in Simulator and the above error "NSArray element failed to match the Swift Array element type" in Xcode. Am I missing something here? Really appreciate any help.

Accepted Reply

>> "NSArray element failed to match the Swift Array element type"


Because your outlet ("letterButtons") really lives in the Obj-C world, it's actually a NSArray, wrapped in a Swift Array value. (This transforms its apparent behavior from a reference type to a value type, but this not relevant to the issue here.)


So, the problem is that you've connected one or more objects to the "letterButtons" outlet that isn't actully a UIButton. I would suggest you try disconnecting everything from that outlet, then connect only the UIButtons you really want it to refer to.

Replies

Probably it is a book for Swift 2.


Try replacing NSArray with Array in types definitions.

Thanks for the quick reply. See problem is there is no "NSArray" in the code. The only place it shows up is in the error when I try to run the app. Also the book says it's the "Swift 4 Edition"....... I'm at a loss?!

Please show the part of code where you get the error

Here is definition of the Array.

Here is where the error occurs

Thanks for your help.

Suggest you study this post: For Best Results - Read the Label


Good luck.

Sorry not sure what happened there....... Guess I'm a total n00b.

Here is the definition of the Array:

@IBOutlet var letterButtons: [UIButton]!


Here is the where the error occurs:

   func enableLetterButtons(_ enable: Bool) {
        for button in letterButtons {
            button.isEnabled = enable
        }
    }

Sorry about that thanks again.

What is your version of XCode ?

Version 9.2 (9C40b)

>> "NSArray element failed to match the Swift Array element type"


Because your outlet ("letterButtons") really lives in the Obj-C world, it's actually a NSArray, wrapped in a Swift Array value. (This transforms its apparent behavior from a reference type to a value type, but this not relevant to the issue here.)


So, the problem is that you've connected one or more objects to the "letterButtons" outlet that isn't actully a UIButton. I would suggest you try disconnecting everything from that outlet, then connect only the UIButtons you really want it to refer to.

That worked! Thanks a lot for your help.

this just started happening to my project after i updated to xcode 10.1 while iterating through scnnode.childnodes.


i've been playing my game for a long time and never ran into this error before.

I have the same problem since i 'v updated, did you find the solution?

nope. sorry for late replies, i hate this site. i hate doing the phone code everytime i want to log in and i hate no notifications when someone replies.

You "hate" this site but you got the answer to your question 😉. Not so bad.


Surprised you have to type the phone code, usually you have to login with your AppleID.


Could re read this:

https://developer.apple.com/support/forums/