Search code examples
swiftoverflowuint16

Swift 2.2: Bug in UInt16?


In my class I define a default port as follows:

private let DEFAULT_PORT: UInt16 = UInt16(47300)

(I also tried : private let DEFAULT_PORT: UInt16 = 47300 )

This should be perfectly ok as the maximum number of UInt16 is 65'535.

The app crashes in one method, though and the debugger shows that DEFAULT_PORT is equal to -18326.

How is this possible? Could it be a bug in Swift? I don't have a clue.


Solution

  • Ok, I investigated a bit more. My problem was not related to the value of DEFAULT_PORT.

    Concerning the negative value in the debugger, it looks to me like a bug in Xcode and not in Swift. I did a few tests and Swift does all operations with the correct value.

    To reproduce anyone can define private let DEFAULT_PORT: UInt16 = UInt16(47300) in the AppDelegate and put a Breakpoint in didFinishLaunchingWithOptions. You should then see -18326 as value in the debugger.