[swift-users] How do you declare a custom NSAttributedStringKey in Objective-C for Swift use?

Jordan Rose jordan_rose at apple.com
Wed Sep 27 11:21:54 CDT 2017


Hi, Marco. The limitation here is that you can’t use swift_name (or NS_SWIFT_NAME) to add members to someone else’s type (more precisely, a type not declared in your module’s headers). You’re not the first to be dissatisfied with this restriction, though, particularly since as you point out you can just add a Swift-side extension to do the same thing. Within Apple, rdar://problem/31877728 <rdar://problem/31877728> tracks making the feature more general.

Similarly, the automatic import-as-member feature for NS_EXTENSIBLE_STRING_ENUMs is also only enabled for constants in the same module as the type. I’m not sure we want to rock the boat on that one.

Jordan


> On Sep 27, 2017, at 07:07, Marco Masser via swift-users <swift-users at swift.org> wrote:
> 
> Hi,
> 
> Swift 4 and the macOS 10.13 SDK added a new NSAttributedStringKey type for the keys that NSAttributedStrings use. The keys are then defined in an extension of NSAttributedStringKey, essentially like this in AppKit:
> 
> // AppKit/NSAttributedString.h (Objective-C)
> extern NSAttributedStringKey NSFontAttributeName;
> 
> // Generated Swift Interface
> extension NSAttributedStringKey {
>     public static let font: NSAttributedStringKey
> }
> 
> 
> How do I get my own custom NSAttributedStringKeys to be imported this way? When I do it like AppKit, it doesn’t seem to work:
> 
> // My Objective-C header
> extern NSAttributedStringKey ODRolloverTokenAttributeName;
> 
> // Generated Swift Interface
> static let ODRolloverTokenAttributeName: NSAttributedStringKey
> 
> 
> That is obviously not the same. I tried using the NS_SWIFT_NAME macro, but that results in the symbol disappearing in Swift completely:
> 
> // My Objective-C header
> extern NSAttributedStringKey ODRolloverTokenAttributeName NS_SWIFT_NAME(NSAttributedStringKey.rolloverToken);
> 
> 
> I also tried to use the swift_name attribute that is used by the NS_SWIFT_NAME macro and that is even mentioned in SE-0044 for exactly this purpose, but the symbol still disappears:
> https://github.com/apple/swift-evolution/blob/master/proposals/0044-import-as-member.md#swift_name-attribute <https://github.com/apple/swift-evolution/blob/master/proposals/0044-import-as-member.md#swift_name-attribute>
> 
> extern const NSAttributedStringKey ODRolloverTokenAttributeName __attribute__((swift_name("NSAttributedStringKey.rolloverToken")));
> 
> 
> What works is to manually define it in an extension like this, but that’s no fun:
> 
> // My Objective-C header
> extern NSAttributedStringKey ODRolloverTokenAttributeName NS_REFINED_FOR_SWIFT;
> 
> extension NSAttributedStringKey {
>     static let rolloverToken = NSAttributedStringKey(__ODRolloverTokenAttributeName.rawValue)
> }
> 
> 
> Is there no way to import this automatically? Was this functionality removed before release even though it was mentioned in SE-0044?
> 
> Cheers,
> 
> Marco
> _______________________________________________
> swift-users mailing list
> swift-users at swift.org
> https://lists.swift.org/mailman/listinfo/swift-users

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20170927/644cc0fa/attachment.html>


More information about the swift-users mailing list