[swift-evolution] Swift's Optional Int as NSNumber in Objective-C

Ben Rimmington me at benrimmington.com
Mon May 22 08:07:36 CDT 2017


> On 20 May 2017, at 19:58, Philippe Hausler wrote:
> 
>> On May 20, 2017, at 12:25 AM, David Waite wrote:
>> 
>> I believe behavior depends on whether the NSNumber is the objc type or swift subtype, and whether you call the NSNumber.intValue or use Int.init?(exactly: NSNumber).
> 
> That part of the behavior is no longer the case. The bridge only transacts upon one number type (to leverage tagged pointers) and the subclass was removed (since it caused some gnarly inconsistencies)

The subclass `_SwiftTypePreservingNSNumber` still exists:

<https://github.com/apple/swift/blob/master/stdlib/public/SDK/Foundation/TypePreservingNSNumber.mm>

Maybe the file isn't compiled anymore due to your pull requests:

<https://github.com/apple/swift/pull/9162>
<https://github.com/apple/swift/pull/9293>

You mentioned tagged pointers. My understanding is that:

* `OBJC_TAG_NSNumber` doesn't attempt to store a `float` or `double` in the 60-bit payload;
* `OBJC_TAG_NSDate` is used only if bits 0...3 of the `NSTimeInterval` (aka `double`) are zero.

<https://opensource.apple.com/source/objc4/objc4-709/runtime/objc-internal.h.auto.html>

How often is an NSDate (with "sub-millisecond precision") created as a tagged pointer?

-- Ben



More information about the swift-evolution mailing list