我想問一些關於在Swift中進行類型轉換的問題。在從父類創建實例時進行類型轉換
有2個班級。
RootViewController
MyViewController
和類層次結構如下圖所示:
class RootViewController: UIViewController {
}
class MyViewController: RootViewController {
}
和,我想簡單地調用instance
函數來創建從廈門國際銀行文件的實例。 所以我在RootViewController
下面實現了函數。
目標C
+ (instancetype)instance {
return [[[self class] alloc] initWithNibName:NSStringFromClass([self class]) bundle:nil];
}
夫特
public class func instance<T:RootViewController>() -> T {
let type = self as UIViewController.Type
let name = NSStringFromClass(type).components(separatedBy: ".").last!
let instance = type.init(nibName: name, bundle: nil)
return instance as! T
}
和,用法是象下面這樣。
Objective-C的
MyViewController *vc = [MyViewController instance];
斯威夫特
let vc = MyViewController.instance() as! MyViewController
問:
我必須一直投實例中使用雨燕的as! MyViewController
的類型? 或者有誰能告訴我一個更好的方法在Swift中?
任何幫助,將不勝感激!
我在這裏看不到任何問題或疑問。我個人會避免在swift中使用'instance'或其他工廠方法,因爲你不能使用'Self'作爲返回類型。改爲使用常規的'init'方法。 – kelin
最好使用「as?」而不是「as!」用於鑄造。 –
而且,我必須補充,這個問題最好在https://codereview.stackexchange.com/上提問 – kelin