苹果文档中 max(by:) 运算符的示例不按描述工作?
example in apple documents for max(by:) operator does not work as described?
我是 Combine/now 学习运算符的新手,
我按照苹果开发者文档中的示例进行操作,但操场上的输出不同
https://developer.apple.com/documentation/combine/publisher/max(by:)
//网站显示
enum Rank: Int {
case ace = 1, two, three, four, five, six, seven, eight, nine, ten, jack, queen, king
}
let cards: [Rank] = [.five, .queen, .ace, .eight, .jack]
cancellable = cards.publisher
.max {
return [=10=].rawValue > .rawValue
}
.sink { print("\([=10=])") }
// Prints: "queen"
我的游乐场显示:
import Combine
enum Rank: Int {
case ace = 1, two, three, four, five, six, seven, eight, nine, ten, jack, queen, king
}
let cards: [Rank] = [.five, .queen, .ace, .eight, .jack]
cards.publisher
.max {
return [=11=].rawValue > .rawValue
}
.sink { print("\([=11=])") }
//Prints: "ace"
我试图解决“数组中最常见的名称”问题,但只有当我使用“<”而不是“>”来查找最大值时它才有效/
import Combine
var subscriptions = Set<AnyCancellable>()
let nameArray: [String] = ["bob", "james", "bob", "james", "lee", "bob", "bob"]
nameArray
.filter{ ![=12=].isEmpty }
.reduce(into: [String: Int]()){ [=12=][] = ([=12=][] ?? 0) + 1 }
.publisher
.max {[=12=].1 < .1 }
.sink{print([=12=])}
.store(in: &subscriptions)
//Prints: "(key: "bob", value: 4)"
难不成苹果文档有误?
还是我做错了什么?
我认为您可能是对的,这是 Apple 文档中的错误。 max
的参数说明是:
areInIncreasingOrder
A closure that receives two elements and returns true if they’re in increasing order.
您会注意到 增加 阶数意味着 [=11=].rawValue
比 .rawValue
少。示例代码使用 >
代替,但仍期望打印 queen
— 这对我来说似乎不正确。如果我将运算符翻转为 <
,它会按预期打印 queen
。
我鼓励您就此文档向 Apple 提交 Feedback。
参见https://www.apeth.com/UnderstandingCombine/operators/operatorsAccumulators/operatorsmax.html。注意解释:
you can supply a function and decide manually whether the parameters are ordered smaller–larger, returning a Bool to indicate whether that’s the case.
(我的斜体。)
但是你是说
return [=10=].rawValue > .rawValue
所以你没有正确回答这些是否按小到大排列的问题。相反,说
return [=11=].rawValue < .rawValue
我是 Combine/now 学习运算符的新手,
我按照苹果开发者文档中的示例进行操作,但操场上的输出不同
https://developer.apple.com/documentation/combine/publisher/max(by:)
enum Rank: Int {
case ace = 1, two, three, four, five, six, seven, eight, nine, ten, jack, queen, king
}
let cards: [Rank] = [.five, .queen, .ace, .eight, .jack]
cancellable = cards.publisher
.max {
return [=10=].rawValue > .rawValue
}
.sink { print("\([=10=])") }
// Prints: "queen"
我的游乐场显示:
import Combine
enum Rank: Int {
case ace = 1, two, three, four, five, six, seven, eight, nine, ten, jack, queen, king
}
let cards: [Rank] = [.five, .queen, .ace, .eight, .jack]
cards.publisher
.max {
return [=11=].rawValue > .rawValue
}
.sink { print("\([=11=])") }
//Prints: "ace"
我试图解决“数组中最常见的名称”问题,但只有当我使用“<”而不是“>”来查找最大值时它才有效/
import Combine
var subscriptions = Set<AnyCancellable>()
let nameArray: [String] = ["bob", "james", "bob", "james", "lee", "bob", "bob"]
nameArray
.filter{ ![=12=].isEmpty }
.reduce(into: [String: Int]()){ [=12=][] = ([=12=][] ?? 0) + 1 }
.publisher
.max {[=12=].1 < .1 }
.sink{print([=12=])}
.store(in: &subscriptions)
//Prints: "(key: "bob", value: 4)"
难不成苹果文档有误? 还是我做错了什么?
我认为您可能是对的,这是 Apple 文档中的错误。 max
的参数说明是:
areInIncreasingOrder
A closure that receives two elements and returns true if they’re in increasing order.
您会注意到 增加 阶数意味着 [=11=].rawValue
比 .rawValue
少。示例代码使用 >
代替,但仍期望打印 queen
— 这对我来说似乎不正确。如果我将运算符翻转为 <
,它会按预期打印 queen
。
我鼓励您就此文档向 Apple 提交 Feedback。
参见https://www.apeth.com/UnderstandingCombine/operators/operatorsAccumulators/operatorsmax.html。注意解释:
you can supply a function and decide manually whether the parameters are ordered smaller–larger, returning a Bool to indicate whether that’s the case.
(我的斜体。)
但是你是说
return [=10=].rawValue > .rawValue
所以你没有正确回答这些是否按小到大排列的问题。相反,说
return [=11=].rawValue < .rawValue