2017-03-04 53 views
1

如何將的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

回答

1

夫特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