2016-05-08 7 views
1

私はコードの目的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)   
    } 
+0

あなたのエラーメッセージの解釈は? – Wain

+0

@Wainコード内にエラーを追加しました。それはinit部分のスーパークラス 'UITableViewCell'の指定された初期化子を呼び出さなければならないと言います。そして、他のエラーは、このエラーの下にあります – Arda

+0

私はあなたが彼らが意味すると思うものを尋ねていることを尋ねました – Wain

答えて

2

あなたがのUITableViewCellのソース内で行く場合は、あなたが表示されます。

public init(style: UITableViewCellStyle, reuseIdentifier: String?) 
public init?(coder aDecoder: NSCoder) 

init()はありませんだからこそ、あなたは指定された初期化子を呼び出す必要がありますエラー。 NSObjectのinitメソッドを呼び出そうとしています。 - あなたは常にあります意味それはfailableないよう自己が、第二の初期化子ではnilであるかどうかを確認する必要がないことを

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() 
} 

注:最初のパスで
、あなたはこのような何かを書くことができます有効で完全に初期化されたオブジェクトです。
しかし、あなたはまだデフォルト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() :)

+0

あなたの助けてくれてありがとうSmnd :)それは働いた! – Arda

+0

恐ろしいです! :) – Smnd

関連する問題