I noticed today that even though the documentation for NSNumberFormatter
stringFromNumber
says that the function declaration takes an NSNumber
as its argument ...
var playerScore = 3456789
let displayScore: String = numberFormatter.stringFromNumber(NSNumber.numberWithInteger(playerScore))
it also works correctly by simply supplying a Swift.Int
...
var playerScore = 3456789
let displayScore: String = numberFormatter.stringFromNumber(playerScore)
Is this the compiler just being clever? or something that should not work. Its things like this that make Swift a little hard to follow at times, especially when its purposely aimed at simplifying things for new developers.
Yes, this is expected behaviour. Swift automatically bridges certain native number types, such as Int
and Float
, to NSNumber
.
According to the Using Swift with Objective-C book, all of the following types are automatically bridged to NSNumber
:
Int
UInt
Float
Double
Bool