Error[8]: Undefined offset: 32, 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(

嵌套数组上的Swift相等运算符

更新: 条件一致性已在 Swift 4.1中 实现 特别是:

现在,标准库类型Optional,Array和Dictionary的元素类型符合Equatable时,它们便符合Equatable协议。…

(来自Swift
CHANGELOG)。

现在可以任意嵌套

Equatable
元素数组,
Equatable
并且可以与进行比较
==
。您的密码

var x: [[Simple]] = [[Simple(message: "a")], [Simple(message: "b")]]var y: [[Simple]] = [[Simple(message: "a")], [Simple(message: "b")]]x == y

如果

Simple
为,则在Xpre 9.3中编译
Equatable


(旧答案:)
其原因与为什么未为可选数组定义Equatable相似。如果元素类型为,则可以 数组进行 比较

==``Equatable


/// Returns true if these arrays contain the same elements.public func ==<Element : Equatable>(lhs: [Element], rhs: [Element]) -> Bool

这就是为什么

var a: [Simple] = [Simple(message: "a")]var b: [Simple] = [Simple(message: "a")]a == b // -> true

编译。

但即使是equatable类型

T
Array<T>
不符合
Equatable
协议,比较为什么我不能做阵列符合Equatable?。因此,在

var x: [[Simple]] = [[Simple(message: "a")], [Simple(message: "b")]]var y: [[Simple]] = [[Simple(message: "a")], [Simple(message: "b")]]x == y // -> ERROR! Binary operator '==' cannot be applied to two '[[Simple]]’ operands

x
y
与元素类型阵列
[Simple]
,其不会 符合
Equatable
协议,并且不存在匹配的
==
运算符。

您可以

==
为简单的嵌套数组定义通用运算符,如下所示:

func ==<Element : Equatable> (lhs: [[Element]], rhs: [[Element]]) -> Bool {    return lhs.count == rhs.count && !zip(lhs, rhs).contains {
func ==<Element : Equatable> (lhs: [[Element]], rhs: [[Element]]) -> Bool {    return lhs.elementsEqual(rhs, by: ==)}
!= }}

或更简单(如@kennytm所建议):

x == y

这样可以进行

==
编译并按预期工作。目前,似乎没有办法[+++]在任意嵌套的数组上定义运算符。



)
File: /www/wwwroot/outofmemory.cn/tmp/route_read.php, Line: 126, InsideLink()
File: /www/wwwroot/outofmemory.cn/tmp/index.inc.php, Line: 166, 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)
嵌套数组上的Swift相等运算符_随笔_内存溢出

嵌套数组上的Swift相等运算符

嵌套数组上的Swift相等运算符,第1张

嵌套数组上的Swift相等运算符

更新: 条件一致性已在 Swift 4.1中 实现 特别是:

现在,标准库类型Optional,Array和Dictionary的元素类型符合Equatable时,它们便符合Equatable协议。…

(来自Swift
CHANGELOG)。

现在可以任意嵌套

Equatable
元素数组,
Equatable
并且可以与进行比较
==
。您的密码

var x: [[Simple]] = [[Simple(message: "a")], [Simple(message: "b")]]var y: [[Simple]] = [[Simple(message: "a")], [Simple(message: "b")]]x == y

如果

Simple
为,则在Xpre 9.3中编译
Equatable


(旧答案:)
其原因与为什么未为可选数组定义Equatable相似。如果元素类型为,则可以 数组进行 比较

==``Equatable


/// Returns true if these arrays contain the same elements.public func ==<Element : Equatable>(lhs: [Element], rhs: [Element]) -> Bool

这就是为什么

var a: [Simple] = [Simple(message: "a")]var b: [Simple] = [Simple(message: "a")]a == b // -> true

编译。

但即使是equatable类型

T
Array<T>
不符合
Equatable
协议,比较为什么我不能做阵列符合Equatable?。因此,在

var x: [[Simple]] = [[Simple(message: "a")], [Simple(message: "b")]]var y: [[Simple]] = [[Simple(message: "a")], [Simple(message: "b")]]x == y // -> ERROR! Binary operator '==' cannot be applied to two '[[Simple]]’ operands

x
y
与元素类型阵列
[Simple]
,其不会 符合
Equatable
协议,并且不存在匹配的
==
运算符。

您可以

==
为简单的嵌套数组定义通用运算符,如下所示:

func ==<Element : Equatable> (lhs: [[Element]], rhs: [[Element]]) -> Bool {    return lhs.count == rhs.count && !zip(lhs, rhs).contains {
func ==<Element : Equatable> (lhs: [[Element]], rhs: [[Element]]) -> Bool {    return lhs.elementsEqual(rhs, by: ==)}
!= }}

或更简单(如@kennytm所建议):

x == y

这样可以进行

==
编译并按预期工作。目前,似乎没有办法在任意嵌套的数组上定义运算符。



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

原文地址: https://outofmemory.cn/zaji/5639368.html

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

发表评论

登录后才能评论

评论列表(0条)

保存