mike vorisis mike vorisis - 6 days ago 5
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.

Answer

Try this :

var mobileApi = MobileAPI.newDemoInstance()

OR

If you don't intend to modify it.

let mobileApi = MobileAPI.newDemoInstance()