Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SR-3146] NS_STRING_ENUM does not produce swift enum #45734

Closed
swift-ci opened this issue Nov 6, 2016 · 6 comments
Closed

[SR-3146] NS_STRING_ENUM does not produce swift enum #45734

swift-ci opened this issue Nov 6, 2016 · 6 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself

Comments

@swift-ci
Copy link
Collaborator

swift-ci commented Nov 6, 2016

Previous ID SR-3146
Radar None
Original Reporter Calimari328 (JIRA User)
Type Bug
Status Resolved
Resolution Won't Do
Environment

Xcode 8 to Xcode 8.1

Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug
Assignee 0-1 (JIRA)
Priority Medium

md5: 3337da0d174cba9207af3de875bc072c

Issue Description:

I believe this is a Huge bug. In Xcode 8 we were given NS_STRING_ENUM and NS_EXTENSIBLE_STRING_ENUM. The former is suppose to turn objective-c Global strings into swift enum strings while the latter turns objective-c Global strings into swift structs with constant variable strings.

Steps to Reproduce:
Suppose we have the following objective-c header file
// traffic.h
typedef NSString * TrafficLightColor NS_STRING_ENUM;

static TrafficLightColor const TrafficLightColorRed = @"Red";
static TrafficLightColor const TrafficLightColorYellow = @"Yellow";
static TrafficLightColor const TrafficLightColorGreen = @"Green";

When we try to access TrafficLightColor in swift, it is treated as a struct. below is the Generated interface produced by Xcode:
struct TrafficLightColor : RawRepresentable, _SwiftNewtypeWrapper, Equatable, Hashable, Comparable, _ObjectiveCBridgeable {
static let red: TrafficLightColor
static let yellow: TrafficLightColor
static let green: TrafficLightColor
}

Expected Results:
According to the documentation, NS_STRING_ENUM should be ported to Swift as an enum.

Actual Results:
Although the documentation says NS_STRING_ENUM should be ported to swift as an enum. It actually gets ported over as a struct

Version:
Xcode 8.0 to 8.1

Notes:
Since NS_STRING_ENUM currently does not create an enum, it is not possible to create an exhaustive list in a switch statement. All switch statements evaluating it will need to end with a default case.

var trafficColor = TrafficLightColor.red
switch trafficColor {
case .red: ()
case .yellow: ()
case .green: ()
default🙁)
// Default case is required. If it was an enum, then all states would have been covered.
}

Notes:
I have 3 sources that claim an NS_STRING_ENUM should produce an enum:

  1. Swift Evolution (SE-0033)
  2. Xcode Quick Help
  3. Apple documentation https://developer.apple.com/library/content/documentation/Swift/Conceptual/BuildingCocoaApps/InteractingWithCAPIs.html
@belkadan
Copy link
Contributor

belkadan commented Nov 7, 2016

It turned out to be neither feasible nor correct to treat NS_STRING_ENUM as a Swift enum. @milseman, can you write up an update to the proposal? (I believe there's already a Radar on the Apple docs folks to update the documentation.)

@swift-ci
Copy link
Collaborator Author

swift-ci commented Nov 8, 2016

Comment by Derrick Ho (JIRA)

I'm a little disappointed that NS_STRING_ENUM turned out to be impossible; its was one of the coolest features of Swift3. @belkadan would the reverse be feasible? By reverse Meaning an enum-string-like swift object can be converted to something for objective-c ?

@belkadan
Copy link
Contributor

belkadan commented Nov 8, 2016

That seems like a reasonable feature request. It would probably have to go through the Swift Evolution Process.

@swift-ci
Copy link
Collaborator Author

Comment by Derrick Ho (JIRA)

That seems like a good idea. I've started by making a template and using the swift mailing list to gauge interest in the feature. https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20161114/028945.html

@milseman
Copy link
Mannequin

milseman mannequin commented Apr 10, 2019

Jordan, do you consider this as behaving correctly or a new feature request?

@belkadan
Copy link
Contributor

Behaving correctly.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself
Projects
None yet
Development

No branches or pull requests

2 participants