Ios Swift中的超级初始化

Ios Swift中的超级初始化,ios,swift,Ios,Swift,我正在尝试将代码目标c转换为swift。我转换了很多,但在代码初始化部分有一个问题 如何修复我的3个错误。删除if语句看起来很容易修复它们。但对于这个问题,我找不到真正的解决办法。 删除if语句是否会导致问题 我的代码: My objective c code: -(id)init { self = [super init]; if (self) // In swift I can delete this statement to solve my problem. Is i

我正在尝试将代码目标c转换为swift。我转换了很多,但在代码初始化部分有一个问题

如何修复我的3个错误。删除if语句看起来很容易修复它们。但对于这个问题,我找不到真正的解决办法。 删除if语句是否会导致问题

我的代码:

My objective c code:

-(id)init
{
    self = [super init]; 
    if (self) // In swift I can delete this statement to solve my problem. Is it causing the problem?
    {
        [self commonInit];
    }
    return self;
}
-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) //Also I can delete this if to solve the error but I am not sure is it the best way
    {
        [self commonInit];
    }
    return self;
}


-(void)commonInit
{
    self.backgroundColor = [UIColor clearColor];
    self.contentView.backgroundColor = [UIColor clearColor];
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    self.accessoryType = UITableViewCellAccessoryNone;

    _textView = [[UITextView alloc] init];

    _timeLabel = [[UILabel alloc] init];
    _statusIcon = [[UIImageView alloc] init];

    [self.contentView addSubview:_timeLabel];
    [self.contentView addSubview:_statusIcon];

}
还有我的swift代码:

init() {

        super.init()  //Gives Error: Must call a designated initializer of the superclass 'UITableViewCell'

        if self != nil { //Value of type 'MessageCell' can never be nil, comparison isn't allowed

            self.commonInit()
        }
    }

    //Added this init because Xcode gives error if not add
    required init?(coder aDecoder: NSCoder) {

        super.init(coder: aDecoder)

        fatalError("init(coder:) has not been implemented")
    }


    override init(style: UITableViewCellStyle, reuseIdentifier: String?){

        super.init(style: style, reuseIdentifier: reuseIdentifier)

        if self != nil { //Error: Value of type 'MessageCell' can never be nil, comparison isn't allowed

          self.commonInit()
        }

    }

 func commonInit() {

        //set values

        self.backgroundColor = UIColor.clearColor()
        self.contentView.backgroundColor = UIColor.clearColor()
        self.selectionStyle = .None
        self.accessoryType = .None

        self.timeLabel = UILabel()
        self.statusIcon = UIImageView()

        self.contentView.addSubview(timeLabel)
        self.contentView.addSubview(statusIcon)        
    }

如果进入UITableViewCell的源代码,您将看到:

public init(style: UITableViewCellStyle, reuseIdentifier: String?)
public init?(coder aDecoder: NSCoder)
没有
init()
,这就是为什么必须调用指定的初始值设定项错误;您正在尝试调用
NSObject
的init方法。
第一次,你可以这样写:

init() {
   super.init(style: UITableViewCellStyle.Default, reuseIdentifier: "example reuse id")
   self.commonInit()
}
override init(style: UITableViewCellStyle, reuseIdentifier: String?){
   super.init(style: style, reuseIdentifier: reuseIdentifier)
   self.commonInit()
}
请注意,无需在第二个初始值设定项中检查self是否为nil,因为它是不可失败的-这意味着您将始终拥有一个有效且完全初始化的对象。
但是,现在有相当多的重复,如果您仍然想要一个默认的
init()
,那么您可以一起去掉commonInit:

convenience init() {
   self.init(style: UITableViewCellStyle.Default, reuseIdentifier: "example reuse id")
}
override init(style: UITableViewCellStyle, reuseIdentifier: String?){
   super.init(style: style, reuseIdentifier: reuseIdentifier)
   //you can initialize your object here, or call your commonInit
}

请注意,如果不需要默认值,也可以去掉第一个
便利init()
:)

您对错误消息的解释是什么?@Wain在代码中添加了错误。它说必须在init部分调用超类“UITableViewCell”的指定初始值设定项。另一个错误在这个错误下面,我看到了,我在问你认为他们是什么意思,真的我不明白为什么会有错误。这是我的自定义类,它继承自UITableViewCell。(MyCell类:UITableViewCell)