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!) 

     } 

    } 

} 

を、Nodeクラスに一般的なワイヤーフレームオブジェクトを追加するためのオプションを追加する方法?

+0

なぜ別の 'Wireframe'ジェネリックパラメータが必要なのですか?あなたのクラスで 'State.WireframeType'型を使うことはできませんか? – Hamish

+0

ハミッシュに感謝します。 –

答えて

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!) 

     } 

    } 

} 
関連する問題