2014-10-17 2 views
18

Xcode 6.0에서 완벽하게 작동하는 사용자 지정 UITableViewCel (멋진 기능 없음)이 있습니다.Xcode 6.1에서 사용 가능한 초기화 도구

class MainTableViewCell: UITableViewCell { 

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) { 
     super.init(style: style, reuseIdentifier: reuseIdentifier) 
     self.setup() 
    } 

    required init(coder aDecoder: NSCoder) { 
     super.init(coder: aDecoder) 
     self.setup() 
    } 

    func setup() {<...>} 
} 

솔루션으로 컴파일러는 Propagate the failure with 'init?' 제안 :

여기 A non-failable initializer cannot chain to failable initializer 'init(style:reuseIdentifier:)' written with 'init?'

셀의 코드입니다 : 내가 엑스 코드 6.1 컴파일러로 컴파일하려고 다음과 같은 오류를 보여줍니다

override init?(style: UITableViewCellStyle, reuseIdentifier: String?) { 
    super.init(style: style, reuseIdentifier: reuseIdentifier) 
    self.setup() 
} 

나는 약간 혼란 스럽다. (non)failable initialiser은 무엇이며 어떻게 사용해야하며 재정의해야합니까? 즉, nil 대신 인스턴스로 반환 할 수 초기화 - 스위프트 1.1

+1

당신은 Xcode 6.0 및 6.1에 대해 사전에 말하고 있습니까? –

+0

예, 당신 말이 맞아요, 거의 밤에 내가있는 곳이고 좀 졸 립니다. :) –

+1

또한, 이해하고있는 것처럼 함수 맨 아래에 super.init 호출을 넣어야합니다. 당신이 재산이 없다면 그건 중요하지 않습니다. –

답변

26

은 (엑스 코드 6.1에서) 애플은 failable 초기화을 소개했다. init 다음에 ?을 넣어서 fiable 이니셜 라이저를 정의합니다. 재정의하려는 이니셜 라이저 엑스 코드 6.0과 6.1 사이의 서명을 변경 :

그래서
// Xcode 6.0 
init(style: UITableViewCellStyle, reuseIdentifier: String?) 

// Xcode 6.1 
init?(style: UITableViewCellStyle, reuseIdentifier: String?) 

당신이 당신의 초기화에 동일한 변경을 확인하고 nil 사건을 처리 할 수 ​​있는지 확인해야합니다 오버라이드 (override) (기준 옵션으로 할당).

failable initializers in Apple's documentation에 대한 자세한 내용을 볼 수 있습니다.

+3

무 처리를 어떻게 처리 하시겠습니까? –

+0

약 6.3 ?? –

관련 문제