ios - Swift 中 'k' 和 'v' 之间的枚举值有什么区别?

标签 ios swift xcode

我今天刚刚更新了最新的 Xcode,当我构建项目时,项目发生了错误。 像这样:

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

枚举定义如下:

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

“玩”的含义含糊不清。 我不知道如何编写来区分这两个“.playing”。

both type of 'playing'

感谢您的回答!

最佳答案

首先回答一下标题中的问题。 “K”表示枚举情况(他们不使用“C”,因为它已经用于“类”)。 “V”表示 var(或 let),即属性。

Objective-C 枚举在 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
    }
    // ...
}

这里的要点是,已弃用的 BJVPlayerStatus_Playing 在 Swift 中被视为计算属性,而不是枚举情况,因此是“V”。这是因为你写了

BJVPlayerStatus_Playing = BJVPlayerStatus_playing

在你的 Objective-C 代码中。

无论如何,在 Swift 中,大写和小写名称都会转换为 playing,这会导致冲突。您需要:

  • 为 Swift 和 Objective-C 代码使用单独的头文件,这样 Swift 代码就不会看到已弃用的情况,或者

  • 使用 NS_SWIFT_NAME 将已弃用案例的 Swift 名称重命名为其他名称。

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

关于ios - Swift 中 'k' 和 'v' 之间的枚举值有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67430453/

相关文章:

ios - 有没有办法在 iOS 设备上调整手电筒/闪光灯的亮度级别?

iphone - iOS 应用程序版本控制

ios - addArrangedSubview 并调整 StackView 的大小

ios - 在钥匙串(keychain)中保存加密的字符串

ios - 如何使 UICollectionViewCell 高度填充框架的所有尺寸?

swift - 从 json 字符串保存实体对象时出现问题 - 如何从可编码继承?

ios - Xcode 字体比例(自动缩小)无法正常工作

ios - 在 OS X 10.10 (Yosemite Beta) 中,如何使用 iOS 6.1 模拟器进行测试?

iphone - 新版本的xcode是否支持跨项目引用以添加静态库进行链接?

ios - 如何使用 CoreImage 创建平滑的阈值过滤器?