ios 从初始值设定项返回之前未调用 Super.init

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30877156/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 06:27:54  来源:igfitidea点击:

Super.init isn't called before returning from initializer

iosuitableviewerror-handlingswift2xcode7

提问by Tom Kuschka

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

我尝试给我的 UITableViewCell 类一个自定义初始化程序,但我无法弄清楚我做错了什么。

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'

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

What can I do? Thank you a lot!

我能做什么?非常感谢!

回答by vrwim

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 initmethods on UITableViewCellis most appropriate.

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

回答by Mir Ashraf

    override init(frame: CGRect) {
        super.init(frame: frame)
    }
    convenience init(frame: CGRect, dataObject: [NSManagedObject]!, objectAttributeValues: [String]!,placeholder: String!, segmentedControl: UISegmentedControl?, cellHeight: CGRect, cellWidth: CGRect){
        self.init(frame: frame)
        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 hope this will help you Override init method of UIView in swift

我希望这会帮助你快速覆盖 UIView 的 init 方法

回答by Mahan Mirshafa

You have to call the superclasses designated initializer.

您必须调用超类指定的初始化程序。

For example, I tried to create a subclass of UIViewand had the exact same problem you had. The designated initializer for UIViewis super.init(frame: CGRect)For UITableViewCellthe designated initializers are as follows.

例如,我尝试创建一个子类UIView并且遇到了与您完全相同的问题。指定初始化的UIViewsuper.init(frame: CGRect)UITableViewCell指定的初始值设定如下。

// Designated initializer.  If the cell can be reused, you must pass in a reuse 
identifier.  You should use the same reuse identifier for all cells of the same 
form.  
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier: 
(nullable NSString *)reuseIdentifier NS_AVAILABLE_IOS(3_0) 
NS_DESIGNATED_INITIALIZER;
- (nullable instancetype)initWithCoder:(NSCoder *)aDecoder 
NS_DESIGNATED_INITIALIZER;

Hope this helped.

希望这有帮助。