swift - Objective-C 枚举符合 RawRepresentable
问题描述
对此我有任何经验,例如以下用 objc 编写的枚举
typedef enum {
Type1,
Type2
} Type;
extension Type: RawRepresentable {
typealias RawValue = UInt32
}
当我试图符合 RawRepresentable 时,编译器崩溃。我唯一可以想象的是 RawRepresentable 仅适用于 swift 枚举。
有任何想法吗?
解决方案
忘记使用原始 C 枚举并使用 Objective-CNS_ENUM
宏:
typedef NS_ENUM(NSInteger, MyEnumType) {
Type1,
Type2
};
然后在 Swift 中,枚举已经是RawRepresentable
. 您不能以这种方式添加该一致性。好吧,您可能可以,但您还必须声明init?(rawValue:)
and var rawValue
。
推荐阅读
- mongodb - mongoDB如何更新多数组数据
- javascript - 如何循环运行我的 div 的全部内容?
- java - 使用 AlertDialog 中的上下文函数从 MainActivity 获取方法的问题
- wget - wget:抓取网页时保存URL
- python-3.x - 如何删除除括号和连字符(-)之外的所有特殊字符
- python-3.x - 在屏幕上放置一个 tkinter 标签几秒钟然后销毁它
- kubernetes - k8s/istio - 在 Envoy 不支持的情况下,服务之间的 HTTP 缓存的替代方案是什么?
- java - 错误:jdk-13.0.2 中不存在包 com.sun.javadoc
- java - 我怎样才能让这个代码打印一个数字中有多少个质数?
- android - 在 Kotlin 中使用协程 IO 线程时使用的主线程