ios - Swift 中的 super 初始化

标签 ios swift

我正在尝试将代码 objective-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
}

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

关于ios - Swift 中的 super 初始化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37098742/

相关文章:

ios - 当键盘快速出现时 UICollectionViewCell 改变高度 4.2

javascript - Action 不会触发 React + Redux 中的 reducer

ios - table view 行可以在其部分移动时保持原样吗?

swift - 如何在 swift 中从 sqlite 检索图像的 id(保存在 BLOB 中)

swift 错误 : Missing return in a function expected to return 'String'

ios - 为什么 FireBase 查询 block 中的 NSString 为 null?

iphone - 从预览内容生成的文档缩略图

ios - 集成解析时在 Xcode 中出错

ios - Swift 动态库的 WatchKit 问题

swift - 检测 UITextField 中的提及不起作用