Use objective-c method in swift seems to cross swift language keyword

I have an objective-c method:

-(DeviceVar *)var:(NSString*)valid 

In objective-c, I just use it like:

 DeviceVar* rtc = [device var:@"rtc"]; 

But in swift, I have a problem using this method:

 let rtc = device.var("etc") 

since var is a keyword, I think, so my question is how to make it work.

+4
source share
1 answer

You can always enclose a reserved word in backticks if you need to use it as a method name (see, for example, Use a reserved keyword for the enum case ):

 let rtc = device.`var`("etc") 

If you have write access to the Objective-C header files, then another option is to specify a different method name for Swift (compare Swift and Objective-C in the same project in "Using Swift with Cocoa and Objective-C Link"):

 -(DeviceVar *)var:(NSString*)valid NS_SWIFT_NAME(deviceVar(_:)); 

which can then be called from Swift as

 let rtc = device.deviceVar("etc") 
+8
source

Source: https://habr.com/ru/post/1258551/


All Articles