[swift-evolution] Proposed changes to SE-0033 Import Objective-C Constants as Swift Types

Michael Ilseman milseman at apple.com
Tue Jun 7 17:58:29 CDT 2016

> On Jun 6, 2016, at 3:51 PM, Ben Rimmington via swift-evolution <swift-evolution at swift.org> wrote:
> SE-0033 and SE-0044 are already "Implemented in Swift 3" according to both
> the proposals and the README, but not according to the bug tracker:
> <https://github.com/apple/swift-evolution/blob/master/README.md>
> <https://bugs.swift.org/browse/SR-1053>
> (I can't find a bug report for SE-0033).
> The example in SE-0033 imports HKQuantityTypeIdentifierBodyMassIndex as the
> HKQuantityTypeIdentifier.bodyMassIndex enum case, but what if you also need
> to nest this inside the class as HKQuantityType.Identifier.bodyMassIndex?

SE0033 is fully compatible with the other enhancements to swift_name (see SE0044), including nesting types and renaming onto types. So in this case you can do something analogous to:

typedef … HKQuantityTypeIdentifier __attribute((swift_struct(extensible))) __attribute((swift_name(“HKQuantityType.Identifier")));

To import the type as a nested type. And, the globals that come in as static members will be members of this nested type (since that’s the only type that exists, that is it’s the Swift type to correspond to HKQuantityTypeIdentifier). A good point for me to clarify is how this interacts with common prefix stripping, common prefix stripping is performed on the C/Objective-C name. If you want full control over the name and where it ends up going, you can use swift_name on the constant itself:

extern const HKQuantityTypeIdentifier HKQuantityTypeIdentifierBodyMassIndex __attribute((swift_name(“HKQuantityType.Identifier.bodyMassIndex")))

Or whatever names / types you want.

Hope this answers your question. Thanks for pointing all of these out, as it deserves mention in any proposal adjustment.

> (See SE-0086 hoisted types).
> -- Ben
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution

More information about the swift-evolution mailing list