swift - 命令因信号 : Segmentation fault: 11 after using generics in graph classes - swift3 而失败

标签 swift generics swift3 xcode8

我试图在 swift 3 中创建一个通用的图形结构。问题是编译器失败并显示以下消息:

While emitting IR SIL function ... for 'init' at .../Graph.swift:48:21

编译器指向的那一行是Edge的init方法类:

public class Edge<T: Hashable, V: Vertex<T>> {
    public var source: V
    public var destination: V
    public let weight: Double?

    required public init(source: V, destination: V, weight: Double? = nil) {
        self.source = source
        self.destination = destination
        self.weight = weight
    }
}

我想我可能对泛型的使用有疑问。 以下是涉及的类:

public class Vertex<T: Hashable> {
    var data: T

    required public init(data: T) {
        self.data = data
    }
}

extension Vertex: Hashable {
    public var hashValue: Int {
        return "\(data)".hashValue
    }

    static public func ==(lhs: Vertex, rhs: Vertex) -> Bool {
        return lhs.data == rhs.data
    }
}

extension Vertex: CustomStringConvertible {
    public var description: String {
        return "\(data)"
    }
}

// MARK: - Edge

public enum EdgeType {
    case directed, undirected
}

public class Edge<T: Hashable, V: Vertex<T>> {
    public var source: V
    public var destination: V
    public let weight: Double?

    required public init(source: V, destination: V, weight: Double? = nil) {
        self.source = source
        self.destination = destination
        self.weight = weight
    }
}

extension Edge: Hashable {
    public var hashValue: Int {
        return "\(source)\(destination)\(weight)".hashValue
    }

    // We need to overload the equals operator because Hashable implements Equatable
    static public func ==(lhs: Edge<T, V>, rhs: Edge<T, V>) -> Bool {
        return lhs.source == rhs.source && lhs.destination == rhs.destination && lhs.weight == rhs.weight
    }
}

这两个类被通用 AdjacencyList<T: Hashable, V: Vertex<T>, E: Edge<T, V>> 使用

谢谢

最佳答案

无法告诉您代码崩溃的原因。您最好针对此问题发送错误报告。但我有一个解决方法:

public protocol VertexType: Hashable {
    associatedtype DataType
    var data: DataType { get set }
}

public class Vertex<T: Hashable>: VertexType {
    public var data: T

    required public init(data: T) {
        self.data = data
    }
}

extension Vertex {
    public var hashValue: Int {
        return "\(data)".hashValue
    }

    static public func ==(lhs: Vertex, rhs: Vertex) -> Bool {
        return lhs.data == rhs.data
    }
}

extension Vertex: CustomStringConvertible {
    public var description: String {
        return "\(data)"
    }
}

// MARK: - Edge

public class Edge<V: VertexType> {
    public var source: V
    public var destination: V
    public let weight: Double?

    required public init(source: V, destination: V, weight: Double? = nil) {
        self.source = source
        self.destination = destination
        self.weight = weight
    }
}

extension Edge: Hashable {
    public var hashValue: Int {
        return "\(source)\(destination)\(weight)".hashValue
    }

    // We need to overload the equals operator because Hashable implements Equatable
    static public func ==(lhs: Edge<V>, rhs: Edge<V>) -> Bool {
        return lhs.source == rhs.source && lhs.destination == rhs.destination && lhs.weight == rhs.weight
    }
}

现在您可以根据需要使用 Vertex 子类:

public class IntVertex: Vertex<Int> {}

Edge(source: IntVertex(data: 1), destination: IntVertex(data: 2))

更新:

看起来这在 Swift 3.1 中已修复。至少相似bug不再使 Xcode 8.3 崩溃。所以可能更新您的 Xcode 就足够了。

关于swift - 命令因信号 : Segmentation fault: 11 after using generics in graph classes - swift3 而失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43055152/

相关文章:

xcode - 当新节点可见时,SceneKit 应用程序卡顿

ios - cellForRowAtIndexPath 不显示 Realm 对象数据

ios - 使 Mapbox RMAnnotation 以编程方式显示其标注

swift - 多色并发症文本

swift3 - 苹果 musickit 用户 token

ios - NSData 到数据 swift 3

ios - 在 Swift 中的自定义单元格之间切换

java - 编写指向其实现者的通用接口(interface)的便捷方法

delphi - "As"约束泛型类型的运算符

java - 为什么 Java 编译器在构造函数实例化中会丢失对泛型类型的跟踪?