[swift-evolution] [pitch] Make swift enum string available to Objc

Kevin Nattinger swift at nattinger.net
Mon Feb 20 18:28:38 CST 2017


I don’t think we need/want to add `@objcstring` or anything like that, but I do think we should let String enums be @objc (currently it’s int types only) and imported as const refs.

// Already works
@objc enum IntEnum: Int {
    case foo = 1
    case bar = 2
}

// "not an integer type"
// Should be allowed.
@objc enum StrEnum: String {
    case foo
    case bar = "baz"
}

Becomes

// Current
typedef NS_ENUM(NSInteger, IntEnum) {
    IntEnumFoo = 1,
    IntEnumBar = 2
};

// Proposed (static or extern, depending on implementation)
NSString *const StrEnumFoo = @“foo";
NSString *const StrEnumBar = @“baz";

In fact, I’d go a step further and say any RawRepresentable enum as a globally initialized const in objc should be allowed the same treatment. Though the only other type that comes to mind is float/double.

> On Feb 20, 2017, at 4:07 PM, Derrick Ho via swift-evolution <swift-evolution at swift.org> wrote:
> 
> Swift should not forsake objective-c.  At least not when it comes enum strings.  Although swift enums are suppose to be swift only, I think we should add a new attribute to slightly relax that.  I think a good attribute would be @objcstring.
> 
> By adding @objcstring, an objective-c exclusive class will be generated.
> 
> @objcstring
> enum Planet {
>   case Jupiter
> }
> 
> I have written up a proposal with more details on what it would look for objective-c.
> 
> https://github.com/wh1pch81n/swift-evolution/blob/swift-enum-objc/proposals/NNNN-Swift-enum-strings-ported-to-Objective-c.md <https://github.com/wh1pch81n/swift-evolution/blob/swift-enum-objc/proposals/NNNN-Swift-enum-strings-ported-to-Objective-c.md>
> 
> If no one objects to this proposal I'll submit it.
> 
> **notes: I am reviving this discussion so that I may submit this for Swift 4 stage 2
> _______________________________________________
> 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/20170220/34b9585e/attachment.html>


More information about the swift-evolution mailing list