Error[8]: Undefined offset: 14, File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 121
File: /www/wwwroot/outofmemory.cn/tmp/plugin_ss_superseo_model_superseo.php, Line: 473, decode(

在实现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 = 
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)
.base as? T { return base == other } else { return false } } }}

[+++]


)
File: /www/wwwroot/outofmemory.cn/tmp/route_read.php, Line: 126, InsideLink()
File: /www/wwwroot/outofmemory.cn/tmp/index.inc.php, Line: 165, include(/www/wwwroot/outofmemory.cn/tmp/route_read.php)
File: /www/wwwroot/outofmemory.cn/index.php, Line: 30, include(/www/wwwroot/outofmemory.cn/tmp/index.inc.php)
在实现Equatable的结构体上进行 *** 作_随笔_内存溢出

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

在实现Equatable的结构体上进行 *** 作,第1张

在实现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 = 
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)
.base as? T { return base == other } else { return false } } }}



欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5476698.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-12
下一篇 2022-12-12

发表评论

登录后才能评论

评论列表(0条)

保存