mike vorisis mike vorisis - 3 months ago 31
iOS Question

How can I initiate an instancetype from Objective C with Swift

I have an api that I have to translate it from Objective C to Swift.
I'm stuck with some type of constructor or initialization I don't really know.

This is how the .h file is:

+ (instancetype) newProductionInstance;
+ (instancetype) newDemoInstance;

This is how .m file is:

+ (instancetype) newProductionInstance
return [[self alloc] initWithBaseURLString:productionURL];

+ (instancetype) newDemoInstance
return [[self alloc] initWithBaseURLString:demoURL];

- (instancetype)initWithBaseURLString:(NSString *)urlString
if (self = [self init])
_apiURL = [NSURL URLWithString:urlString];
return self;

This is the call that they have to the main file I'm translating:

mobileApi = [MobileAPI newDemoInstance];

So I want to convert only the last line to Swift 2.

Thanks in advance.


Try this :

var mobileApi = MobileAPI.newDemoInstance()


If you don't intend to modify it.

let mobileApi = MobileAPI.newDemoInstance()