2017-09-27 5 views
3

저는 신속하게 제네릭을 잘못 이해하고 있습니다. 비 명목 형 X는 명시 적 초기화를 지원하지 않습니다.

나는이 샘플 놀이터를 만들어

import UIKit 

public protocol MainControllerToModelInterface : class { 
    func addGoal() 
    init() 
} 

public protocol MainViewControllerInterface : class { 
    associatedtype MODELVIEW 
    var modelView: MODELVIEW? {get set} 

    init(modelView: MODELVIEW) 
} 

public class MainViewController<M> : UIViewController, MainViewControllerInterface where M : MainControllerToModelInterface { 
    public weak var modelView: M? 

    required public init(modelView: M) { 
     self.modelView = modelView 
     super.init(nibName: String(describing: MainViewController.self), bundle: Bundle.main) 
    } 

    required public init?(coder aDecoder: NSCoder) { 
     fatalError("init(coder:) has not been implemented") 
    } 
} 

public class Other<C, M> : NSObject where C : MainViewControllerInterface, C : UIViewController, M : MainControllerToModelInterface, C.MODELVIEW == M { 
    var c : C? 

    override init() { 
     let m = M() 
     self.c = C(modelView: m) 
     super.init() 
    } 
} 

라인 self.c = C(modelView: m) 내가 나이가 Xcode의 버전이 오류가

cannot invoke initializer for type '%type' with an argument list of type '...' expected an argument list of type '...'

을 의미 볼 this other stack overflow 질문에서 나에게이 오류 non-nominal type 'C' does not support explicit initialization

을 제공합니다 하지만 위의 놀이터에서 컴파일러 mi는 무엇입니까? ssing?

나는 swift4/xcode9에 있습니다.

업데이트

의 제안을 Use C.init(modelView: m) rather than C(modelView: m) 오류 변화를 다음과 후 : @보다

No 'C.Type.init' candidates produce the expected contextual result type '_?'

VINI - 응용 프로그램이 작동하는지 확인하기 위해있는 UIViewController을 제거하기 위해 제안했다. UIViewController가있을 때 나는 왜 컴파일러가 행복하지 않은지 아직 이해하지 못한다. C가 그 유효한 init 메소드를 가지고 있다는 것을 아는 것으로 충분하지 않습니까?

self.c = C.init(modelView: m) 

답변

9

당신은 명시 적으로 init를 사용해야합니다. 그것은 그것을 고쳐야한다.

+0

감사합니다, 나는'얻기보다 'C.Type.init'후보가 예상 문맥을 생산하는 이유가의 ViewController로 C 취급하고

init(modelView: M) 그게 전부 같은의 ViewController에는 init 없다 결과 유형 '_?' ' –

+0

내 주요 프로젝트에서이 문제가 실제로 해결되었습니다. –

1

사용 C.init(modelView: m) 오히려 C(modelView: m)보다 : 당신이 아니라 "진짜"형식이 아닌 일반 매개 변수를 초기화 할 때마다

+0

감사합니다.'No 'C.Type.init'후보가 예상 문맥 결과 유형 '_?'을 생성합니다. –

+0

내 주요 프로젝트에서이 문제가 실제로 해결되었습니다. 그러나 첫 번째이기 때문에 다른 대답에 수표를주었습니다. 다시 한 번 감사드립니다! –

1

확인하십시오 : 당신의 코드에서

을이 C : MainViewControllerInterface, C : UIViewController 같이하고있다. 그 던지기 오류

public class Other<C, M> : NSObject where C : MainViewControllerInterface, M : MainControllerToModelInterface, C.MODELVIEW == M { 
    var c : C? 

    override init() { 
     let m = M() 
     self.c = C(modelView: m) 
     super.init() 
    } 
} 
+0

고맙지 만, C는'C : MainViewControllerInterface'입니다. 이 프로토콜은 내가 사용하고있는 이니셜 라이저를 정의합니다. –

+0

ViewController를 삭제 한 후 작동합니다. 제 코드를 확인하십시오. –

+0

네, 그래도 여전히 그것이 필요한 것 같습니다. 당신은'그것은 ViewController로서 C를 다루고있다. 그렇다면 init (modelView : M) that that that throwing error'와 같은 ViewController에 init이 없다는 것이 나에게 의미가 없다. C는 또한'MainViewControllerInterface'를 채택하고 있는데, 왜 컴파일러가 충분하지 않습니까? –