[swift-users] Importing NSUInteger in my own Objective-C Code as Int

Marco Masser lists at duckcode.com
Sat Jan 16 06:28:14 CST 2016


> On 2016-01-15, at 19:19, Jens Alfke <jens at mooseyard.com> wrote:
> 
>> On Jan 15, 2016, at 5:19 AM, Marco Masser via swift-users <swift-users at swift.org <mailto:swift-users at swift.org>> wrote:
>> 
>> Swift imports NSUInteger in Objective-C APIs as Int, but only for Frameworks like Foundation and AppKit. For other code, NSUInteger is imported simply as UInt.
> 
> Really? I’d never noticed that. It seems wrong to me, since NSUInteger is clearly an unsigned type, so it should map to Uint.

That is actually intentional on Apple’s part. I think it was mentioned in the WWDC sessions when Swift was introduced (don’t remember which one) and the “Using Swift with Cocoa and Objective-C” book mentions it since the very first release, if I’m not mistaken. To quite the section Foundation Data Types <https://developer.apple.com/library/ios/documentation/Swift/Conceptual/BuildingCocoaApps/WorkingWithCocoaDataTypes.html#//apple_ref/doc/uid/TP40014216-CH6-ID75>:

> Swift bridges NSUInteger and NSInteger to Int. Both of these types come over as Int in Foundation APIs. Int is used for consistency whenever possible in Swift, but the UInt type is available if you require an unsigned integer type.


Clearly, Foundation APIs (and other Apple frameworks) get a special treatment here. What I’m looking for is a way to get that for my code, too.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20160116/55d83ff7/attachment.html>


More information about the swift-users mailing list