-1

Trying to read data in Swift (2) from an NSDictionary with Xcode 7.3 I came across the infamous EXC_BAD_INSTRUCTION (code=EXCI386_INVOP,subcode=0X0) error when trying this:

let aDict = data as! NSDictionary
car.tempo = aDict["tempo"] as! Int32

No compiler warnings though. I am aware that there were issues with Xcode, Swift and Int32s in the past. Any suggestions how to handle this better? TIA!

Since it wasn't clear maybe - data as shown above is guaranteed to contain data from a plist file and aDict["tempo"] is an NSNumber.

caxix
  • 1,085
  • 1
  • 13
  • 25
  • "Use the forced form of the type cast operator (as!) only when you are sure that the downcast will always succeed. This form of the operator will trigger a runtime error if you try to downcast to an incorrect class type." –  May 24 '16 at 02:48
  • 1
    Or to put it another way, any time you use `!` you're pretty much asking for a crash, so you need to be **really** sure about what you're doing. – Tom Harrington May 24 '16 at 03:17

2 Answers2

0

You did not set value to aDict for key "tempo"...

Archie
  • 150
  • 6
-1

Thanks for the suggestions. There was no issue regarding the data and if so there should not have been a EXC_BAD_INSTRUCTION (code=EXCI386_INVOP,subcode=0X0) error. This is apparently a bug in Xcode and I will file a radar. Reformulating like below does work in Xcode 7.3 as NSNumber gets unwrapped as an Int32:

car.tempo = (aDict["tempo"]?.intValue)!

Thanks again!

caxix
  • 1,085
  • 1
  • 13
  • 25
  • Well no, because you changed what the code is doing to avoid the issue. This isn't a bug in Xcode, it's a bug in your code that you fixed. Before you were attempting to downcast `NSNumber` to `Int32`, which is impossible, but the `as!` forced the issue so the code crashed. Now you're calling `intValue` on the `NSNumber`, which means you're asking for the `NSNumber`'s integer value instead of trying to force an object to downcast to a primitive type. Incidentally this is **exactly** what @PetahChristian and I described in our comments. – Tom Harrington May 24 '16 at 17:51
  • Congrats on the double down vote! Your original comments just stated that exclamation marks are dangerous. Had you stated that a downcast from an NSNumber to an Int32 is impossible - different story. But what you said was obviously not helpful in the least. – caxix May 25 '16 at 21:08
  • I didn't downvote this question or your answer. My comment didn't mention `NSNumber` because you didn't indicate that you were using one. Using a `!` is always taking a major risk of a crash, which is why I said that you needed to be sure about what you were doing. Clearly you were not, but your code change fixed it. The original issue is still a bug in your code and not in Xcode. – Tom Harrington May 25 '16 at 21:28
  • Apologies then! Thought it would have been clear that a numeric value in an NSDictionary had to be an NSNumber. Being new to Swift I was wrongly under the impression that unwrapping via NSNumber as! Int32 was equivalent to using intValue on it. I will have to review bridging of NSNumber to Int, etc. I am aware of the inherent dangers of the ! operator, but am still getting used to the different set and sometimes lack of compiler warnings in Swift. In any case the bug got fixed and thanks for the help. – caxix May 25 '16 at 22:08