pbm
pbm

Reputation: 5371

NS_ENUM to Swift 3 not working as expected

I'm using Swift 3 and xcode 8. I'm very new to objective-C and fairly new to swift, so maybe this is an idiot question. But here goes!

I'm bridging from objective-C to swift 3. Here's a code snippet of the objective-C:

typedef NS_ENUM(NSInteger, MaplyMapType) {
    MaplyMapType3D,
    MaplyMapTypeFlat,
};

@interface MaplyViewController : MaplyBaseViewController

/// @brief Initialize as a flat or 3D map.
- (nonnull instancetype)initWithMapType:(MaplyMapType)mapType;

In my Swift 3 source file I instantiate a MaplyViewController. The following is the WORKING code (it compiles and runs, and no xcode errors).

theViewC = MaplyViewController(mapType: .typeFlat)

Why does this work? From references https://developer.apple.com/library/content/documentation/Swift/Conceptual/BuildingCocoaApps/InteractingWithCAPIs.html and other references I think the correct code should be:

theViewC = MaplyViewController(mapType: .flat)

But that won't compile.

MORE INFO FROM ONE DAY LATER: Amin Negm-Awad in his comment below has almost explained this. The simple bridging rules cannot be used to produce identifiers .flat and .3D because identifier .3D is not allowed. The language reference says "Identifiers begin with an uppercase or lowercase letter A through Z, an underscore (_), a noncombining alphanumeric Unicode character in the Basic Multilingual Plane, or a character outside the Basic Multilingual Plane that isn’t in a Private Use Area. After the first character, digits and combining Unicode characters are also allowed."

So the bridging cannot result in enum identifiers .flat and .3d. I could not find any reference to explain the rules bridging uses to come up with the alternative identifies, namely .typeFlat and .type3D in this case.

Upvotes: 2

Views: 212

Answers (1)

Amin Negm-Awad
Amin Negm-Awad

Reputation: 16660

Likely it is, because the other identifier (MaplyMapType3D) would be 3Dinstead of type3D, but identifiers must not start with a digit. So one has to keep type.

Upvotes: 1

Related Questions