栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

在实现Equatable的结构体上进行操作

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

在实现Equatable的结构体上进行操作

正如错误所指出的那样,问题是,您不能将具有“自我”或相关类型要求的协议用作实际类型,因为您会丢失有关这些要求的类型信息。在这种情况下,您将丢失

==
实现参数的类型信息,因为
Equatable
它们必须与一致类型(即
Self
)具有相同的类型。

解决方案几乎总是建立一个类型的橡皮擦。在期望类型相等的情况下,如果它们的

id
属性相等,则可以简单地
id
将其存储起来并在
==
实现中进行比较。

struct AnyVehicle : Equatable {    static func ==(lhs: AnyVehicle, rhs: AnyVehicle) -> Bool {        return lhs.id == rhs.id    }    let id : String    init<T : Vehicle>(_ base: T) {        id = base.id    }}

(请注意,为了符合Swift命名约定,我将您的

ID
属性重
id
命名为)

但是,更通用的解决方案是在类型橡皮擦中存储一个函数,该函数可以在类型转换之后

Vehicle
根据
==
实现比较两个任意符合的实例,以确保它们与创建类型橡皮擦的具体类型相同。 。

struct AnyVehicle : Equatable {    static func ==(lhs: AnyVehicle, rhs: AnyVehicle) -> Bool {        // forward to both lhs's and rhs's _isEqual in order to determine equality.        // the reason that both must be called is to preserve symmetry for when a        // superclass is being compared with a subclass.        // if you know you're always working with value types, you can omit one of them.        return lhs._isEqual(rhs) || rhs._isEqual(lhs)    }    let base: Identifiable    private let _isEqual: (_ to: AnyVehicle) -> Bool    init<T : Vehicle>(_ base: T) {        self.base = base        _isEqual = { // attempt to cast the passed instance to the concrete type that // AnyVehicle was initialised with, returning the result of that // type's == implementation, or false otherwise. if let other = $0.base as? T {     return base == other } else {     return false }        }    }}

print(AnyVehicle(Car(id: "foo")) == AnyVehicle(Tractor(id: "foo"))) // falseprint(AnyVehicle(Car(id: "foo")) == AnyVehicle(Car(id: "bar"))) // falseprint(AnyVehicle(Car(id: "foo")) == AnyVehicle(Car(id: "foo"))) // truevar array = [AnyVehicle]()array.append(AnyVehicle(Car(id: "VW")))array.append(AnyVehicle(Car(id: "Porsche")))array.append(AnyVehicle(Tractor(id: "John Deere")))array.append(AnyVehicle(Tractor(id: "Steyr")))var op = Operator()// compiles fine as AnyVehicle conforms to Equatable.op.operationOnCollectionOfEquatables(array: array)


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/484558.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号