在从初始化程序返回之前不会调用Super.init [英] Super.init isn't called before returning from initializer

查看:210
本文介绍了在从初始化程序返回之前不会调用Super.init的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图给我的UITableViewCell类一个自定义的启动,但我无法弄清楚我在做错了什么。



这是我的代码:

  init(dataObject:[NSManagedObject ]!,objectAttributeValues:[String] !,占位符:String !, segmentedControl:UISegmentedControl?,cellHeight:CGRect,cellWidth:CGRect){
self.dataObject = dataObject
self.Placeholder.text = placeholder
self.objectAttributeValues = objectAttributeValues

如果segmentedControl!= nil {
self.segmentedControl = segmentedControl!
didHaveSegmentedControl = true
}

}

必需的init(coder aDecoder:NSCoder){
fatalError(init(coder :)没有实现)
}

我试图调用super.init(frame: CGRect(...)),但通过实现这个我得到另一个错误:必须调用超类UITableViewCell的指定的初始化程序



什么我可不可以做?
非常感谢!

解决方案

初始化工具的工作原理是,他们将添加自己的属性,常量和函数到那个实例,然后调用它的类型的对象的超类。更多信息这里



因此,您必须在退出初始化器之前调用超类初始化。在这里,我建议您在初始化器的最后一行调用 super.init()。您可以选择在 UITableViewCell 上的哪个 init 方法是最合适的。


I try to give my UITableViewCell Class a custom initilizer but i can't figure it out what I am doing wrong.

Here is my Code:

init(dataObject: [NSManagedObject]!, objectAttributeValues: [String]!,placeholder: String!, segmentedControl: UISegmentedControl?, cellHeight: CGRect, cellWidth: CGRect) {
    self.dataObject = dataObject
    self.Placeholder.text = placeholder
    self.objectAttributeValues = objectAttributeValues

    if segmentedControl != nil {
        self.segmentedControl = segmentedControl!
        didHaveSegmentedControl = true
    }

}

required init(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

I tried to call super.init(frame: CGRect(...)) but by implementing this I get another error: Must call a designated initializer of the superclass 'UITableViewCell'

What can I do? Thank you a lot!

解决方案

The way initialisers work, is they will add their own properties, constants and functions to that instance, then call back to the superclass for an object of it's type. More info here.

For this reason you must call a superclass' initialiser before exiting the initialiser. Here I suggest you call super.init() on the last line of your initialiser. You can choose which of the init methods on UITableViewCell is most appropriate.

这篇关于在从初始化程序返回之前不会调用Super.init的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆