Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

New Swift 5 warnings for Objective-C enums: how to get rid of them?

As of Xcode 10.2, when using enums I've defined in Objective-C, but in a Swift 5 switch statement, I get the following warning, even if I've exhausted all the possible enum values.

Switch covers known cases, but 'MyObjectiveCEnumName' may have additional 
unknown values

Xcode is telling me I should fix this by

Handle unknown values using "@unknown default"

Why is this happening and what can I do about it?


Example

Objective-C enum

typedef NS_ENUM(NSUInteger, CardColor) {
  CardColorBlack,
  CardColorRed
};

Swift 5 switch statement

var cardColor: CardColor = .black

switch (cardColor) {
case .black:
  print("black")
case .red:
  print("red")
}
like image 676
Clay Bridges Avatar asked Mar 28 '19 15:03

Clay Bridges


People also ask

Can I use Swift enum in Objective C?

From what I understand, you can only import Swift stuff in . m files and there is no way to forward declare an enum in Objective C.

Should enums be capitalized Swift?

The name of an enum in Swift should follow the PascalCase naming convention in which the first letter of each word in a compound word is capitalized.

How do I create an enum in Objective C?

Objective-C Language Enums Defining an enumtypedef NS_ENUM(NSUInteger, MyEnum) { MyEnumValueA = 0, MyEnumValueB = 5, MyEnumValueC = 10, }; You can also specify on the first value and all the following will use it with increment: typedef NS_ENUM(NSUInteger, MyEnum) { MyEnumValueA = 0, MyEnumValueB, MyEnumValueC, };

What are enums Swift?

In Swift, an enum (short for enumeration) is a user-defined data type that has a fixed set of related values. We use the enum keyword to create an enum. For example, enum Season { case spring, summer, autumn, winter } Here, Season - name of the enum.


1 Answers

TL;DR

If you want Objective-C enums to be treated just like Swift ones, you now need to declare them using a different macro, NS_CLOSED_ENUM, versus the old NS_ENUM. Changing this will make the warning disappear.

The example above would become

typedef NS_CLOSED_ENUM(NSUInteger, CardColor) {
  CardColorBlack,
  CardColorRed
};

Deets

From the Swift 5 release notes:

In Swift 5 mode, switches over enumerations that are declared in Objective-C or that come from system frameworks are required to handle unknown cases—cases that might be added in the future, or that may be defined privately in an Objective-C implementation file. Formally, Objective-C allows storing any value in an enumeration as long as it fits in the underlying type. These unknown cases can be handled by using the new @unknown default case, which still provides warnings if any known cases are omitted from the switch. They can also be handled using a normal default case.

If you’ve defined your own enumeration in Objective-C and you don’t need clients to handle unknown cases, you can use the NS_CLOSED_ENUM macro instead of NS_ENUM. The Swift compiler recognizes this and doesn’t require switches to have a default case.

like image 73
Clay Bridges Avatar answered Sep 28 '22 05:09

Clay Bridges