Skip to content

Instantly share code, notes, and snippets.

@venj
Last active November 3, 2015 11:21
Show Gist options
  • Save venj/3b6bce6112711105b33f to your computer and use it in GitHub Desktop.
Save venj/3b6bce6112711105b33f to your computer and use it in GitHub Desktop.
let number:NSNumber? = NSNumber(longLong: 3_000_000_000) // May came from Objective-C
guard let unwrappedNumber = number as? Int else { return }
print(unwrappedNumber)
// get -1294967296 on 32-bit platform
let numberStr = "3000000000"
let anotherNumber = Int(numberStr)
print("\(anotherNumber)")
// get nil on 32-bit platform
guard let unwrappedLong = number?.longLongValue else { return }
print("\(unwrappedLong)")
// now works on both 32-bit and 64 bit.
// unwrappedLong is Int64 on 32-bit platform
// Int and Int64 is actually the same on 64-bit platform
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment