Objective-C NS_ENUM are not working as expected when used with Swift

Originator:fantattitude
Number:rdar://23664652 Date Originated:25-Nov-2015
Status:Closed Resolved:No
Product:Developer Tools Product Version:Version 7.1.1 (7B1005)
Classification:Serious bug Reproducible:Always
 
Summary:
When importing an ObjC file containing an NS_ENUM in a project using Swift, initializing a variable with .init(rawValue:) method of the enum, first the result is never nil and second if the rawValue provided is "invalid" because not one of the possible values defined in the ObjC file the object still gets created and match any of the case of the Enum when used in a switch clause.

Steps to Reproduce:
1. Create an empty Swift project (CLI works great)
2. Add an ObjC file containing an NS_ENUM to the project (along with the bridging header to use the enum in your Swift.
3. Init a variable using said enum with a rawValue not specified in the NS_ENUM declaration
4. Get angry because it gets defined and match any case when used in a Swift switch clause

Expected Results:
Work the same way the corresponding Swift enum work.
nil when rawValue is wrong.

Actual Results:
Variable gets a value and worst it matches each case of a switch.

Version:
Version 7.1.1 (7B1005)

Notes:
Swift is awesome. I joined a really simple & clean project showing the issue ;)

Configuration:


Attachments:
'ObjcEnumImportBug.zip' was successfully uploaded.

Comments

Vivien Leroy — 02-Dec-2015 05:17 PM

Shouldn't ObjC enums imported in Swift then be marked as such (with @objc for example) and switch statements on these enum types must have a default instead of matching any other case ?

By fantattitude at Dec. 2, 2015, 4:17 p.m. (reply...)

Apple Developer Relations — 01-Dec-2015 11:24 PM

There are no plans to address this based on the following:

Unfortunately, some C enums are defined using NS_ENUM, but have additional values that appear at runtime. This could be because certain cases are considered Apple-internal, or because your app is running on a newer OS that has added new cases. This is legal according to the C standard.

(This is important so that “MyEnum(rawValue: someEnumInstance.rawValue)” is always a safe round-trip operation.)

We are now closing this report.

If you have questions about the resolution, or if this is still a critical issue for you, then please update your bug report with that information.

By fantattitude at Dec. 2, 2015, 4:12 p.m. (reply...)

Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!