[swift-evolution] SE proposal: Add @objc to enum strings
Jay Abbott
jay at abbott.me.uk
Sun Nov 20 13:59:40 CST 2016
It seems like a sensible/reasonable approach, but personally I don't care
about accessing Swift from Obj-C, or writing Swift that can be called from
Obj-C.
On Sun, 20 Nov 2016 at 18:15 Derrick Ho via swift-evolution <
swift-evolution at swift.org> wrote:
> Does anyone have any thoughts on this idea?
>
> I Made a draft proposal of this very idea
>
> https://github.com/wh1pch81n/swift-evolution/blob/master/proposals/NNNN-Swift-enum-strings-ported-to-Objective-c
>
> On Sat, Nov 19, 2016 at 2:44 PM Derrick Ho <wh1pch81n at gmail.com> wrote:
>
> [TL;DR]
> SE-0033 ports objective-c global strings to swift enums, but the reverse
> seems to be missing. There should be a swift construct that can be
> accessible by objective-c.
>
> [Proposal]
> In a previously implemented swift proposal, the concept of NS_STRING_ENUM
> and NS_EXTENSIBLE_STRING_ENUM was born. (see:
> https://github.com/apple/swift-evolution/blob/master/proposals/0033-import-objc-constants.md
> )
>
> This would enable objective-c global strings to be ported over to swift as
> an enum and struct respectively.
>
> I always found it odd that this interoperability only worked in one
> direction. I think that enum strings should be accessible to Objective-c
> when it is marked with the @objc attribute. When an enum string has @objc
> it should be ported over to objective-c as global strings.
>
> // Swift example
> @objc
> public enum Food: String {
> case Calamari
> case Fish
> }
>
> // Objective-c port
> typedef NSString *_Nonnull Food;
> static Food const Food_Calimari = @"Calimari";
> static Food const Food_Fish = @"Fish";
>
> The Objective-c port code could be added as part of the generated header
> file or a framework's umbrella header.
>
> When a structs is given the attribute @objcextstring it should become
> available to objective-c as a class. This attribute will put restrictions
> on the struct such that it only has static constants and a mandatory
> property called rawValue.
>
> // Swift example
> @objcextstring
> public struct Planets {
> public let rawValue: String
> public static let Earth = Planets(rawValue: "Earth")
> public static let Venus = Planets(rawValue: "Venus")
> }
>
> // Objective-c port
> @interface Planets: NSObject
> + (instanceType)Earth;
> + (instanceType)Venus;
> @end
>
> @implementation Planets
> + (instanceType)Earth {
> return [[Planets alloc] initWithRawValue: @"Earth"];
> }
> + (instanceType)Venus {
> return [[Planets alloc] initWithRawValue:@"Venus"];
> }
> @end
>
> What do you guys thinks of this proposal? Do you think it will enhance
> the objective-c to swift interoperability?
>
>
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20161120/0dacb545/attachment.html>
More information about the swift-evolution
mailing list