Objc 可见字符串枚举但不是 RawRepresentable
Objc visible string enum but not RawRepresentable
我想使用在 objective C 和 Swift 中都可见但不符合协议 RawRepresentable 的枚举。
我试图让一个字符串枚举在 Objc 和 Swift 中都可见,因此我使用
typedef NSString *myEnum NS_TYPED_ENUM;
我试图利用 myEnum(rawValue: ) -> myEnum?功能,但我发现 enumType 已自动符合
public struct myEnum : Hashable, Equatable, RawRepresentable {
public init(rawValue: String)
}
我的问题是如何创建在 Objc 和 Swift 中可见但不符合此协议的枚举?感谢大家的帮助!
Swift Language Enhancements
... Swift enums can now be exported to Objective-C using the @objc
attribute. @objc enums must declare an integer raw type, and cannot be
generic or use associated values. Because Objective-C enums are not
namespaced, enum cases are imported into Objective-C as the
concatenation of the enum name and case name.
为此,您在 Objective-C 中定义值,您可以使用 NS_TYPED_ENUM
宏在 Swift 中导入常量
例如:
.h 文件
typedef NSString *const ProgrammingLanguage NS_TYPED_ENUM;
FOUNDATION_EXPORT ProgrammingLanguage ProgrammingLanguageSwift;
FOUNDATION_EXPORT ProgrammingLanguage ProgrammingLanguageObjectiveC;
.m 文件
ProgrammingLanguage ProgrammingLanguageSwift = @"Swift";
ProgrammingLanguage ProgrammingLanguageObjectiveC = @"ObjectiveC";
在 Swift 中,这是作为结构导入的:
struct ProgrammingLanguage: RawRepresentable, Equatable, Hashable {
typealias RawValue = String
init(rawValue: RawValue)
var rawValue: RawValue { get }
static var swift: ProgrammingLanguage { get }
static var objectiveC: ProgrammingLanguage { get }
}
虽然该类型没有桥接为枚举,但在Swift代码中使用它时感觉非常相似。
的“与 C API 交互”中阅读有关此技术的更多信息
我想使用在 objective C 和 Swift 中都可见但不符合协议 RawRepresentable 的枚举。
我试图让一个字符串枚举在 Objc 和 Swift 中都可见,因此我使用
typedef NSString *myEnum NS_TYPED_ENUM;
我试图利用 myEnum(rawValue: ) -> myEnum?功能,但我发现 enumType 已自动符合
public struct myEnum : Hashable, Equatable, RawRepresentable { public init(rawValue: String) }
我的问题是如何创建在 Objc 和 Swift 中可见但不符合此协议的枚举?感谢大家的帮助!
Swift Language Enhancements
... Swift enums can now be exported to Objective-C using the @objc attribute. @objc enums must declare an integer raw type, and cannot be generic or use associated values. Because Objective-C enums are not namespaced, enum cases are imported into Objective-C as the concatenation of the enum name and case name.
为此,您在 Objective-C 中定义值,您可以使用 NS_TYPED_ENUM
宏在 Swift 中导入常量
例如:
.h 文件
typedef NSString *const ProgrammingLanguage NS_TYPED_ENUM;
FOUNDATION_EXPORT ProgrammingLanguage ProgrammingLanguageSwift;
FOUNDATION_EXPORT ProgrammingLanguage ProgrammingLanguageObjectiveC;
.m 文件
ProgrammingLanguage ProgrammingLanguageSwift = @"Swift";
ProgrammingLanguage ProgrammingLanguageObjectiveC = @"ObjectiveC";
在 Swift 中,这是作为结构导入的:
struct ProgrammingLanguage: RawRepresentable, Equatable, Hashable {
typealias RawValue = String
init(rawValue: RawValue)
var rawValue: RawValue { get }
static var swift: ProgrammingLanguage { get }
static var objectiveC: ProgrammingLanguage { get }
}
虽然该类型没有桥接为枚举,但在Swift代码中使用它时感觉非常相似。
的“与 C API 交互”中阅读有关此技术的更多信息