简体   繁体   中英

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

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'.

两种类型的“玩”

Thanks for your answer!

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 ), ie 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM