简体   繁体   中英

Can I use NS_SWIFT_NAME or NS_REFINED_FOR_SWIFT on a C enum to map a global function to a Swift property?

I have an enum like

typedef NS_ENUM(NSInteger, MyEnum) {
  MyEnumCase1,
  MyEnumCase2,
  ...
};

and a function that maps those enum values to arbitrary strings

FOUNDATION_EXPORT NSString *myEnumString(MyEnum val);

Is it possible to expose this to Swift as a property?

I've tried

FOUNDATION_EXPORT NSString *myEnumString(MyEnum val) NS_SWIFT_NAME(MyEnum.stringVal);

but the compiler gives me the warning "'swift_name' attribute argument must be a string literal specifying a Swift function name" and Swift callers don't see stringVal as a property on values of MyEnum .

and I've tried

FOUNDATION_EXPORT NSString *myEnumString(MyEnum val) NS_REFINED_FOR_SWIFT;

but my Swift extension

extension MyEnum {
  var stringVal {
    return __myEnumString(self)
  }
}

can't find __myEnumString() .

The Swift function name passed to NS_SWIFT_NAME must include the implicit self: argument, and to import it as a read-only property (instead of a method), "getter:" must be prepended. Various examples can be found in SE-0044 Import as member .

So the correct syntax to expose the C function as a (read-only) property to Swift is

NSString *myEnumString(MyEnum val)
NS_SWIFT_NAME(getter:MyEnum.stringVal(self:));

The generated Swift interface is

extension MyEnum {
    public var stringVal: String { get }
}

and this compiles and runs as expected in my test:

print(MyEnum.case1.stringVal)

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