2016-10-11 11 views
0

다음 작업을 수행하는 방법을 잘 모르겠습니다. (스위프트 3, XCode8).Swift 3의 일반 유형 강요

상태 개체 및/또는 와이어 프레임 개체를 일반 매개 변수로 사용하고 상태 개체가 NodeState라는 프로토콜 제약 조건을 갖는 일반 Node 클래스를 만들려고합니다. 다음 코드와

Cannot convert value of type Node<State, Wireframe> to type Node<_,_> in coercion 

(놀이터에서 작동합니다) : 어떤 도움을 크게 감상 할 수

import Foundation 

public protocol NodeState { 

    associatedtype EventType 
    associatedtype WireframeType 

    mutating func action(_ event: EventType, withNode node: Node<Self, WireframeType>) 

} 

extension NodeState { 

    mutating public func action(_ event: EventType, withNode node: Node<Self, WireframeType>) { } 

} 

public class Node<State: NodeState, Wireframe> { 

    public var state: State 
    public var wireframe: Wireframe? 

    public init(state: State, wireframe: Wireframe?) { 

     self.state = state 

     guard wireframe != nil else { return } 
     self.wireframe = wireframe 

    } 

    public func processEvent(_ event: State.EventType) { 

     DispatchQueue.main.sync { [weak self] in 

      // Error presents on the following 

      let node = self! as Node<State, State.WireframeType> 

      self!.state.action(event, withNode: node) 

     } 

    } 

} 

나는 다음과 같은 오류가 발생합니다입니다. 감사!

UPDATE :

다음 작품 - 나는 와이어 프레임 참조 제거 할 때 : 지금

import Foundation 

public protocol NodeState { 

    associatedtype EventType 

    mutating func action(_ event: EventType, withNode node: Node<Self>) 

} 

extension NodeState { 

    mutating public func action(_ event: EventType, withNode node: Node<Self>) { } 

} 

public class Node<State: NodeState> { 

    public var state: State 

    public init(state: State) { 

     self.state = state 

    } 

    public func processEvent(_ event: State.EventType) { 

     DispatchQueue.main.sync { [weak self] in 

      self!.state.action(event, withNode: self!) 

     } 

    } 

} 

을 어떻게 노드 클래스에 일반적인 와이어 프레임 객체를 추가하는 옵션을 추가하려면?

+0

왜 'Wireframe' 일반 매개 변수가 필요한가요? 클래스에서'State.WireframeType' 타입을 사용할 수 없습니까? – Hamish

+0

감사합니다 Hamish, 그 작품. –

답변

0

내가 의심하는 것을 적용하면 해밋 (Hamish)의 대답이 주어지며, 현재 예상대로 컴파일됩니다. 감사!

import Foundation 

public protocol NodeState { 

    associatedtype EventType 
    associatedtype WireframeType 

    mutating func action(_ event: EventType, withNode node: Node<Self>) 

} 

extension NodeState { 

    mutating public func action(_ event: EventType, withNode node: Node<Self>) { } 

} 


public class Node<State: NodeState> { 

    public var state: State 
    public var wireframe: State.WireframeType? 

    public init(state: State, wireframe: State.WireframeType?) { 

     self.state = state 

     guard wireframe != nil else { return } 
     self.wireframe = wireframe 

    } 

    public func processEvent(_ event: State.EventType) { 

     DispatchQueue.main.sync { [weak self] in 

      self!.state.action(event, withNode: self!) 

     } 

    } 

}