将Obj-C初始化覆盖函数转换为Swift 3

问题描述:

如何将的initWithDevice函数改写为MyViewControllerB转换为Swift 3?将Obj-C初始化覆盖函数转换为Swift 3

MyViewControllerA

@interface MyViewControllerA : UIViewController 
- (instancetype)initWithDevice:(DeviceClass *)device; 
@end 

@implementation MyViewControllerA 
- (instancetype)initWithDevice:(DeviceClass *)device 
{ 
    self = [super init]; 
    if (self) { 
     // set a bunch of properties 
    } 
    return self; 
} 
@end 

MyViewControllerB

@interface MyViewControllerB : MyViewControllerA 
    - (instancetype)initWithDevice:(DeviceClass *)device; 
@end 

@implementation MyViewControllerB 
- (instancetype)initWithDevice:(DeviceClass *)device 
{ 
    if (self = [super initWithNibName:@"AnotherViewController" bundle:nil]) { 
     // init stuff for subclass 
    } 
    return self; 
} 
@end 
+0

试试这个http://objc.to/jlzy3h –

+0

试试这个https://objectivec2swift.com/ #/ home/converter/ –

+0

谢谢,伙计们!漂亮的工具。 @AntonyRaphel - 我也不得不增加以下内容: 所需的init(编码器aDecoder:NSCoder){ super.init(编码器:aDecoder) } 如果你想发布的解决方案,我会接受?谢谢! – Vee

夫特3.0夫特2.2

override init(device: DeviceClass) { 
super.init(nibName: "AnotherViewController", bundle: nil) 

// init stuff for subclass 

} 

required init?(coder aDecoder: NSCoder){ 
    super.init(coder: aDecoder) 
} 

您可以与您Objective-C的代码试图进入斯威夫特通过使用此网站也:objectivec2swift