【问题标题】:Generic type coercion in Swift 3Swift 3 中的泛型类型强制
【发布时间】:2016-10-11 09:53:42
【问题描述】:

我不确定如何进行以下工作。 (斯威夫特 3,XCode8)。

我正在尝试制作将状态对象和/或线框对象作为通用参数的通用 Node 类,其中状态对象具有作为 NodeState 的协议约束。

我收到以下错误:

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

使用以下代码(应该在 Playground 中工作):

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)

        }

    }

}

任何帮助将不胜感激。谢谢!

更新:

以下工作 - 当我删除线框引用时:

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 类中添加用于添加通用线框对象的选项?

【问题讨论】:

  • 为什么需要一个单独的Wireframe 泛型参数?你不能在你的课堂上使用State.WireframeType这个类型吗?
  • 感谢 Hamish,这行得通。

标签: generics type-conversion swift3 swift-protocols type-alias


【解决方案1】:

应用我怀疑是 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!)

        }

    }

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多