2016-11-30 4 views
2

Objective CからSwiftに翻訳する必要があるAPIがあります。 私は実際にはわからないコンストラクタや初期化のいくつかのタイプに固執しています。SwiftでObjective CからInstancetypeを開始するにはどうすればいいですか

これは.hファイルがどのようにある:

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

これがどのように.Mファイルです:

+ (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; 
} 

これは私が翻訳してるメインファイルへの彼らが持っている呼び出しです:

mobileApi = [MobileAPI newDemoInstance]; 

だから私はスウィフト2.

に最後の行だけを変換したいです

ありがとうございます。

答えて

2
var mobileApi = MobileAPI.newDemoInstance() 

または

let mobileApi = MobileAPI.newDemoInstance() 

あなたはそれを変更する予定がない場合。

+0

感謝に役立ちます願っています。 (私は4分以内に受け入れます) –

1

単純にMobileAPI.newDemoInstance()です。

let mobileApi = MobileAPI.newDemoInstance() 

注:は​​ファイルにMobileAPI.hをインポートするのを忘れないでください。

1

私は、これが働い

class YourClass: NSObject { 
    //Class level constants 
    static let productionURL = "YourProductionURL" 
    static let demoURL = "YourDemoURL" 

    //Class level variable 
    var apiURL : String! 

    //Static factory methods 
    static func newProductionInstance() -> YourClass { 
     return YourClass(with : YourClass.productionURL) 
    } 

    static func newDemoInstance() -> YourClass { 
     return YourClass(with : YourClass.demoURL) 
    } 

    // Init method 
    convenience init(with baseURLString : String) { 
     self.init() 
     self.apiURL = baseURLString 

     //Calling 
     let yourObject : YourClass = YourClass.newDemoInstance() 
    } 
} 
+0

*と一緒に、間違いを指摘してくれた小文字 – user28434

+1

@ user28434から始める必要があります。 –

関連する問題