"NSNumber" does not convert to "Int" Swift

I am trying to do the math to set the position of a shape node. It looks like this (Swift): var score: NSNumber? score = score! + 1

xcode 6 beta 5 works fine, but xocde 6.1 won't start, "NSnumer" report won't convert to "Int"

+3


source to share


1 answer


I realize this is an older question and you probably figured it out by now, but having similar issues, I figured I'd post an answer for posterity.

Swift (as far as I know) is still undergoing design changes as a programming language, and one thing I noticed was that I needed to start wrapping my int inside a function Int()

. Therefore, your example might need to look something like this:

var score: NSNumber?
score = NSNumber(integer:score!) + 1

      



Edit:

To try and explain why we should be doing this, I believe it is because score

- it is NSNumber()

, which can have various number forms. NSNumber(integer:score!)

re-wraps score!

as an integer, in particular, allowing us to use integers in it in mathematical equations.

+5


source







All Articles