Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use objective-c method in swift seems crossing keyword of swift language

I have an objective-c method:

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

In objective-c I simple use it as:

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

But in swift I have a problem using this method:

let rtc = device.var("etc")

as var is a keyword I guess, so my question is how to make it work.

like image 960
Matrosov Oleksandr Avatar asked Aug 29 '16 09:08

Matrosov Oleksandr


People also ask

Can I use Objective-C in Swift?

You can use Objective-C and Swift files together in a single project, no matter which language the project used originally. This makes creating mixed-language app and framework targets as straightforward as creating an app or framework target written in a single language.

What is difference between Objective-C and Swift?

Swift is a general-purpose, high-level programming language which is highly concerned about safety, performance. Objective C is an general purpose language which is considered as superset of C language it was designed in an aim of providing object-oriented capabilities.


1 Answers

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

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

If you have write access to the Objective-C header files then another option is to define a different method name for Swift (compare Swift and Objective-C in the Same Project in the "Using Swift with Cocoa and Objective-C " reference):

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

which can then be called from Swift as

let rtc = device.deviceVar("etc")
like image 133
Martin R Avatar answered Oct 13 '22 21:10

Martin R