<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class="">Hi, Marco. The limitation here is that you can’t use swift_name (or NS_SWIFT_NAME) to add members to someone <i class="">else’s</i> 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, <a href="rdar://problem/31877728" class="">rdar://problem/31877728</a> tracks making the feature more general.</div><div class=""><br class=""></div><div class="">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.</div><div class=""><br class=""></div><div class="">Jordan</div><div class=""><br class=""></div><br class=""><div><blockquote type="cite" class=""><div class="">On Sep 27, 2017, at 07:07, Marco Masser via swift-users <<a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><meta http-equiv="Content-Type" content="text/html; charset=utf-8" class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class="">Hi,<div class=""><br class=""></div><div class="">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:</div><div class=""><br class=""></div><div class=""><div style="margin: 0px; font-stretch: normal; font-size: 13px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0); background-color: rgb(255, 255, 255);" class="">// AppKit/NSAttributedString.h (Objective-C)</div><div style="margin: 0px; font-stretch: normal; font-size: 13px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""><span style="color: rgb(186, 45, 162);" class="">extern</span> <span style="color: rgb(112, 61, 170);" class="">NSAttributedStringKey</span> NSFontAttributeName;</div></div><div class=""><br class=""></div><div class=""><div style="margin: 0px; font-stretch: normal; font-size: 13px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0); background-color: rgb(255, 255, 255);" class="">// Generated Swift Interface</div></div><div class=""><div style="margin: 0px; font-stretch: normal; font-size: 13px; line-height: normal; font-family: Menlo; color: rgb(112, 61, 170); background-color: rgb(255, 255, 255);" class=""><span style="color: #ba2da2" class="">extension</span><span style="" class=""> </span>NSAttributedStringKey<span style="" class=""> {</span></div><div style="margin: 0px; font-stretch: normal; font-size: 13px; line-height: normal; font-family: Menlo; color: rgb(112, 61, 170); background-color: rgb(255, 255, 255);" class=""><span style="" class=""> </span><span style="color: #ba2da2" class="">public</span><span style="" class=""> </span><span style="color: #ba2da2" class="">static</span><span style="" class=""> </span><span style="color: #ba2da2" class="">let</span><span style="" class=""> font: </span>NSAttributedStringKey</div></div><div style="margin: 0px; font-stretch: normal; font-size: 13px; line-height: normal; font-family: Menlo; color: rgb(112, 61, 170); background-color: rgb(255, 255, 255);" class=""><span style="" class="">}</span></div><div style="margin: 0px; font-stretch: normal; font-size: 13px; line-height: normal; font-family: Menlo; color: rgb(112, 61, 170); background-color: rgb(255, 255, 255);" class=""><br class=""></div><div style="margin: 0px; font-stretch: normal; font-size: 13px; line-height: normal; font-family: Menlo; color: rgb(112, 61, 170); background-color: rgb(255, 255, 255);" class=""><span style="font-family: Helvetica; font-size: 12px;" class=""><br class=""></span></div><div style="margin: 0px; font-stretch: normal; font-size: 13px; line-height: normal; font-family: Menlo; color: rgb(112, 61, 170); background-color: rgb(255, 255, 255);" class=""><span style="font-family: Helvetica; font-size: 12px;" class="">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:</span></div><div style="margin: 0px; font-stretch: normal; font-size: 13px; line-height: normal; font-family: Menlo; color: rgb(112, 61, 170); background-color: rgb(255, 255, 255);" class=""><br class=""></div><div style="margin: 0px; font-stretch: normal; line-height: normal; background-color: rgb(255, 255, 255);" class=""><div style="color: rgb(0, 132, 0); font-family: Menlo; font-size: 13px; margin: 0px; font-stretch: normal; line-height: normal;" class="">// My Objective-C header</div><div style="font-family: Menlo; font-size: 13px; margin: 0px; font-stretch: normal; line-height: normal;" class=""><span style="color: rgb(186, 45, 162);" class="">extern</span> <span style="color: #703daa" class="">NSAttributedStringKey</span> ODRolloverTokenAttributeName;</div><div style="font-family: Menlo; font-size: 13px; margin: 0px; font-stretch: normal; line-height: normal;" class=""><br class=""></div><div style="font-family: Menlo; font-size: 13px; margin: 0px; font-stretch: normal; line-height: normal;" class=""><span style="color: rgb(0, 132, 0);" class="">// Generated Swift Interface</span></div><div style="margin: 0px; font-stretch: normal; line-height: normal;" class=""><div style="font-family: Menlo; font-size: 13px; margin: 0px; font-stretch: normal; line-height: normal;" class=""><span style="color: #ba2da2" class="">static</span> <span style="color: #ba2da2" class="">let</span> ODRolloverTokenAttributeName: <span style="color: #703daa" class="">NSAttributedStringKey</span></div><div style="font-family: Menlo; font-size: 13px; margin: 0px; font-stretch: normal; line-height: normal;" class=""><span style="color: #703daa" class=""><br class=""></span></div><div style="font-family: Menlo; font-size: 13px; margin: 0px; font-stretch: normal; line-height: normal;" class=""><span style="color: #703daa" class=""><br class=""></span></div><div style="margin: 0px; font-stretch: normal; line-height: normal;" class=""><div style="color: rgb(112, 61, 170); font-family: Menlo; font-size: 13px; margin: 0px; font-stretch: normal; line-height: normal;" class=""><span style="font-family: Helvetica; font-size: 12px;" class="">That is obviously not the same. I tried using the NS_SWIFT_NAME macro, but that results in the symbol disappearing in Swift completely:</span></div><div style="color: rgb(112, 61, 170); font-family: Menlo; font-size: 13px; margin: 0px; font-stretch: normal; line-height: normal;" class=""><span style="font-family: Helvetica; font-size: 12px;" class=""><br class=""></span></div><div style="color: rgb(112, 61, 170); font-family: Menlo; font-size: 13px; margin: 0px; font-stretch: normal; line-height: normal;" class=""><div style="margin: 0px; font-stretch: normal; line-height: normal;" class=""><span style="color: rgb(0, 132, 0);" class="">// My Objective-C header</span></div><div style="margin: 0px; font-stretch: normal; line-height: normal;" class=""><span style="color: #ba2da2" class="">extern</span> <span style="color: #703daa" class="">NSAttributedStringKey</span> ODRolloverTokenAttributeName <span style="color: #78492a" class="">NS_SWIFT_NAME</span>(NSAttributedStringKey.rolloverToken);</div></div><div style="font-family: Menlo; font-size: 13px;" class=""><span style="font-family: Helvetica; font-size: 12px;" class=""><br class=""></span></div><div style="font-family: Menlo; font-size: 13px;" class=""><span style="font-family: Helvetica; font-size: 12px;" class=""><br class=""></span></div><div class="">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:</div><div class=""><a href="https://github.com/apple/swift-evolution/blob/master/proposals/0044-import-as-member.md#swift_name-attribute" class="">https://github.com/apple/swift-evolution/blob/master/proposals/0044-import-as-member.md#swift_name-attribute</a></div><div style="font-family: Menlo; font-size: 13px;" class=""><span style="font-family: Helvetica; font-size: 12px;" class=""><br class=""></span></div><div style="font-family: Menlo; font-size: 13px;" class=""><div style="margin: 0px; font-stretch: normal; line-height: normal;" class=""><span style="color: #ba2da2" class="">extern</span> <span style="color: #ba2da2" class="">const</span> <span style="color: #703daa" class="">NSAttributedStringKey</span> ODRolloverTokenAttributeName <span style="color: #ba2da2" class="">__attribute__</span>((swift_name(<span style="color: #d12f1b" class="">"NSAttributedStringKey.rolloverToken"</span>)));</div></div><div style="font-family: Menlo; font-size: 13px;" class=""><span style="font-family: Helvetica; font-size: 12px;" class=""><br class=""></span></div><div class=""><div style="margin: 0px; font-stretch: normal; line-height: normal;" class=""><br class=""></div><div style="margin: 0px; font-stretch: normal; line-height: normal;" class="">What works is to manually define it in an extension like this, but that’s no fun:</div></div><div style="margin: 0px; font-stretch: normal; line-height: normal;" class=""><br class=""></div><div style="margin: 0px; font-stretch: normal; line-height: normal;" class=""><div style="margin: 0px; font-stretch: normal; font-size: 13px; line-height: normal; font-family: Menlo;" class=""><span style="color: rgb(0, 132, 0);" class="">// My Objective-C header</span></div><div style="margin: 0px; font-stretch: normal; font-size: 13px; line-height: normal; font-family: Menlo;" class=""><span style="color: #ba2da2" class="">extern</span> <span style="color: #703daa" class="">NSAttributedStringKey</span> ODRolloverTokenAttributeName <span style="color: #78492a" class="">NS_REFINED_FOR_SWIFT</span>;</div><div style="margin: 0px; font-stretch: normal; font-size: 13px; line-height: normal; font-family: Menlo;" class=""><br class=""></div><div style="margin: 0px; font-stretch: normal; line-height: normal;" class=""><div style="font-family: Menlo; font-size: 13px; margin: 0px; font-stretch: normal; line-height: normal; color: rgb(112, 61, 170);" class=""><span style="color: #ba2da2" class="">extension</span><span style="" class=""> </span>NSAttributedStringKey<span style="" class=""> {</span></div><div style="font-family: Menlo; font-size: 13px; margin: 0px; font-stretch: normal; line-height: normal;" class=""> <span style="color: rgb(186, 45, 162);" class="">static</span> <span style="color: rgb(186, 45, 162);" class="">let</span> rolloverToken = <span style="color: rgb(112, 61, 170);" class="">NSAttributedStringKey</span>(<span style="color: rgb(79, 129, 135);" class="">__ODRolloverTokenAttributeName</span>.<span style="color: rgb(112, 61, 170);" class="">rawValue</span>)</div><div style="font-family: Menlo; font-size: 13px; margin: 0px; font-stretch: normal; line-height: normal;" class="">}</div><div style="font-family: Menlo; font-size: 13px;" class=""><br class=""></div><div style="font-family: Menlo; font-size: 13px;" class=""><span style="font-family: Helvetica; font-size: 12px;" class=""><br class=""></span></div><div style="font-family: Menlo; font-size: 13px;" class=""><span style="font-family: Helvetica; font-size: 12px;" class="">Is there no way to import this automatically? Was this functionality removed before release even though it was mentioned in SE-0044?</span></div><div style="font-family: Menlo; font-size: 13px;" class=""><span style="font-family: Helvetica; font-size: 12px;" class=""><br class=""></span></div><div style="font-family: Menlo; font-size: 13px;" class=""><span style="font-family: Helvetica; font-size: 12px;" class="">Cheers,</span></div><div style="font-family: Menlo; font-size: 13px;" class=""><span style="font-family: Helvetica; font-size: 12px;" class=""><br class=""></span></div><div style="font-family: Menlo; font-size: 13px;" class=""><span style="font-family: Helvetica; font-size: 12px;" class="">Marco</span></div></div></div></div></div></div></div>_______________________________________________<br class="">swift-users mailing list<br class=""><a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a><br class="">https://lists.swift.org/mailman/listinfo/swift-users<br class=""></div></blockquote></div><br class=""></body></html>