简体   繁体   English

Swift UIViewController自定义init() - 错误无法分配给self

[英]Swift UIViewController Custom init() - Error cannot assign to self

In Objective-C I used to override the init method of my UIViewController . 在Objective-C中,我用来覆盖我的UIViewControllerinit方法。 I cannot achieve the same in Swift : 我无法在Swift中实现同样的目标:

Objective-C code : Objective-C代码:

- (instancetype)init
{
    self = [super init];
    if (self) {
        self = [[UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]] instantiateViewControllerWithIdentifier:@"ViewController"];
    }
    return self;
}

If I try to do so in Swift I get errors for "Cannot assign to self". 如果我尝试在Swift中这样做,我会收到“无法分配给自己”的错误。 I have already implemented the initWithCoder: method just to get through a few more errors. 我已经实现了initWithCoder:方法,只是为了解决一些错误。

Can someone please tell me how can I port the above Objective-C code to Swift to get the desired behavior. 有人可以告诉我如何将上述Objective-C代码移植到Swift以获得所需的行为。

I want to init the ViewController from storyboard. 我想从storyboard初始化ViewController。

You cannot assign self in init . 你不能在init分配self You should use class method or public function instead. 您应该使用class方法或公共函数。

class func viewControllerFromStoryboard() -> ViewController? {
    let storyboard = UIStoryboard(name: "Main", bundle: NSBundle(forClass: ViewController.self))
    if let controller = storyboard.instantiateViewControllerWithIdentifier("ViewController") as? ViewController
    {
        return controller
    }
    return nil
}

After you can call 你打电话之后

let controller = ViewController.viewControllerFromStoryboard()

Init method in swift is different from the ones in objc. swift中的init方法与objc中的方法不同。

In objc the only requirement for init method is that the initializing method begins with the letters “init” . 在objc the only requirement for init method is that the initializing method begins with the letters “init” You create a instance in this method and assign to pointer self 您在此方法中创建一个实例并分配给指针self

whereas in swift Initializing is the process of preparing an instance of a class, structure, or enumeration for use . 而在swift中, Initializing is the process of preparing an instance of a class, structure, or enumeration for use It's kind of like you already has a self point to an instance. 这有点像你已经有一个实例的self指向。 What you do in init method is set up self's properties and other 你在init方法中做的是设置self的属性和其他

That's why you cannot assign to self in swift for class 这就是为什么你不能在课堂上快速分配给自己的原因

but you can assign to self in a mutating method of struct 但你可以在struct的变异方法中赋予self

I suggest you write like this 我建议你这样写

class func customInit -> XXXController {
    var vc: XXXController = XXXController()
    //  write something that you want to initialize 
    return vc 
}

just like you write objc 就像你写objc一样

- (instancetype)initCustom {
    self = [super init];
    if (self) {
       //  write something that you want to initialize 
    }
    return self; 
}

and you can cell 你可以细胞

var customVc: XXXController = XXXController.customInit()

just like 就像

XXXController *vc = [[XXXController alloc] initCustom]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM