私はコードの目的cを迅速に変換しようとしています。私はそれらの多くを変換するが、私はコードのinit部分に問題がある。スウィフトのスーパーinit
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)
}
あなたのエラーメッセージの解釈は? – Wain
@Wainコード内にエラーを追加しました。それはinit部分のスーパークラス 'UITableViewCell'の指定された初期化子を呼び出さなければならないと言います。そして、他のエラーは、このエラーの下にあります – Arda
私はあなたが彼らが意味すると思うものを尋ねていることを尋ねました – Wain