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

Marco Masser lists at duckcode.com
Fri Jan 15 07:19:37 CST 2016


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. Is there some way to get those as Int in Swift for my Objective-C code, too? Maybe some macro in the spirit of NS_REFINED_FOR_SWIFT, NS_SWIFT_NAME, and NS_SWIFT_NOTHROW? Or probably rather something that gets applied to each parameter or return type? I didn’t find anything like that.

For example, I have an NSSegmentedCell subclass that has the following methods:

- (void)setRepresentedObject:(nullable id)anObject forSegment:(NSUInteger)segment;
- (nullable id)representedObjectForSegment:(NSUInteger)segment;

… which come through to Swift as:

func setRepresentedObject(anObject: AnyObject?, forSegment segment: UInt)
func representedObjectForSegment(segment: UInt) -> AnyObject?

Note the UInt instead of Int. This isn’t very nice because all of the NSSegmentedCell methods use Int. Also, writing code involving these methods and indices extracted from an Array isn’t nice because it requires constant conversions between UInt and Int.

A nice solution would be to have something that allows selectively annotating these parameter and return types to tell the Clang Importer how to import types like NSUInteger to Swift. I guess Frameworks like Foundation and AppKit use something like that internally to opt-in or opt-out of this conversion.
Is there something like that I can use? Am I missing something?

Cheers,

Marco
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20160115/904bf5af/attachment.html>


More information about the swift-users mailing list