iosobjective-cswiftswift2instancetype

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.


Solution

  • var mobileApi = MobileAPI.newDemoInstance()
    

    or

    let mobileApi = MobileAPI.newDemoInstance()
    

    if you don't intend to modify it.