What's the Enum value difference between 'k' and 'v' in Swift?

Viewed 85

I just updated the latest Xcode today, when I build my project, the project was occurred an error. Like this:

let playerStatus: BJYPlayerStatus = .playing // ambiguous use of 'playing'

The enum defined like this:

typedef NS_ENUM (NSInteger, BJVPlayerStatus) {
    BJVPlayerStatus_playing,
    // other cases...
    
    BJVPlayerStatus_Playing DEPRECATED_MSG_ATTRIBUTE("use `BJVPlayerStatus_playing`") =
        BJVPlayerStatus_playing
    // other deprecated cases...
};

It is ambiguous about the 'playing'. I don't know how to write to distinguish both of the '.playing'.

both type of 'playing'

Thanks for your answer!

1 Answers

First, to answer the question in the title. "K" means an enum case (they don't use "C" because it's used for "class" already). "V" means a var (or let), i.e. a property.

The Objective-C enum gets treated like this in Swift:

// This is not real Swift code, for illustrative purposes only
enum BJVPlayerStatus : Int {
    // This was BJVPlayerStatus_playing
    case playing
    // ...

    // This was BJVPlayerStatus_Playing
    @available(*, deprecated, message: "use `BJVPlayerStatus_playing`")
    var playing: BJVPlayerStatus {
        return BJVPlayerStatus.playing // returns the *case* .playing
    }
    // ...
}

The point here is that the deprecated BJVPlayerStatus_Playing is treated as a computed property in Swift, rather than an enum case, hence the "V". This is because you wrote

BJVPlayerStatus_Playing = BJVPlayerStatus_playing

in your Objective-C code.

Anyway, both the uppercase and lowercase name translates to playing in Swift, and that causes the conflict. You need to either:

  • use separate header files for the Swift and Objective-C code, so the Swift code doesn't see the deprecated cases, or

  • rename the Swift name for the deprecated case to something else using NS_SWIFT_NAME.

      BJVPlayerStatus_Playing NS_SWIFT_NAME(deprecatedPlaying) DEPRECATED_MSG_ATTRIBUTE("use `BJVPlayerStatus_playing`") = 
          BJVPlayerStatus_playing
    
Related