我想问一些关于在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