I am trying to adopt a SDK written in objective-c in a swift project. The objective-c way to initialize the SDK is as follows:
@implementation ViewController
nokeSDK *nokelock;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//INITIALIZE NOKE SDK
nokelock = [[nokeSDK alloc] init];
nokelock->cmd = [[nokecommand alloc] init];
I don't believe there is an equivalent to the arrow operator in swift, so is it possible to still initialize? I can't seem to find any info about his particular subject.
In Objective-C, a property is merely syntactic sugar for accessor methods; if, as is usually the case, these are a front for an instance variable, you can, with the right privacy access, bypass the accessor methods (or not have them in the first place) and get/set the instance variable directly. That's what your code is doing.
But Swift doesn't draw that distinction. A variable declared at the top level of a type declaration, in Swift, is a property; behind the scenes, a stored property has accessor methods, and you are passing through those automatically when you get/set the property. There is no separate instance variable to get/set.
To see what I mean, make a hybrid Swift / Objective-C app with class Thing whose interface looks like this:
@interface Thing : NSObject {
@public
NSString* s;
}
Now create the generated interface. You will see that, in Swift, s
is nowhere to be seen.
Presumably, therefore, to rewrite your code, you'd need to turn cmd
into a property, or at least provide a setter method. Otherwise, Swift will never be able to set the Objective-C cmd
instance variable. Of course, if it is also up to you to rewrite nokeSDK
in Swift, your problems are over, as you can now do whatever you like.