Improved NSNumber

With earlier versions of Swift, behavior of NSNumber might be unexpected and casting to Uint8 might result in absurd results:

let number1 = NSNumber(value: 1000)let number1ConvertedToUInt = number1 as? UInt8

In this scenario, logically, the value inside number1ConvertedToUInt should be nil, but this was not the case and the value would be 1000% 255, that is, 232 instead. This is because the maximum value that a UInt can hold is 255. Fortunately, this behavior has been resolved in Swift 4 and now if you execute the same code in Swift 4, you should expect the value of number1ConvertedToUInt to be nil.

Get Reactive Programming with Swift 4 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.