我正在嘗試將包含 KeyPath 和排序順序型別的多個元組傳遞給應該進行排序的方法。
我有這個方法:
extension Array {
mutating func sort<T: Comparable>(by criteria: (path: KeyPath<Element, T>, order:OrderType)...) {
criteria.forEach { path, order in
//...
sort { first, second in
order.makeComparator()(
first[keyPath: path],
second[keyPath: path]
)
}
}
}
}
我正在這樣使用它:
var posts = BlogPost.examples
posts.sort(by:(path:\.pageViews, order: .asc), (path:\.sessionDuration, order: .desc))
現在,因為pageViews和sessionDuration屬性都是integers,這將起作用。
但是如果我想傳遞兩個不同型別的屬性(比如String和Int),我會收到這個錯誤:
Key path value type 'Int' cannot be converted to contextual type 'String'
這是其余的代碼,但我想不是那么相關:
enum OrderType: String {
case asc
case desc
}
extension OrderType {
func makeComparator<T: Comparable>() -> (T, T) -> Bool {
switch self {
case .asc:
return (<)
case .desc:
return (>)
}
}
}
我應該如何定義排序方法以便它接受異構鍵路徑?
uj5u.com熱心網友回復:
沒有可變引數泛型(還),所以你需要寫一個AnyComparable型別橡皮擦。這個改編自這里的這篇文章。
struct AnyComparable: Equatable, Comparable {
private let lessThan: (Any) -> Bool
private let value: Any
private let equals: (Any) -> Bool
public static func == (lhs: AnyComparable, rhs: AnyComparable) -> Bool {
lhs.equals(rhs.value) || rhs.equals(lhs.value)
}
public init<C: Comparable>(_ value: C) {
self.value = value
self.equals = { $0 as? C == value }
self.lessThan = { ($0 as? C).map { value < $0 } ?? false }
}
public static func < (lhs: AnyComparable, rhs: AnyComparable) -> Bool {
lhs.lessThan(rhs.value) || (rhs != lhs && !rhs.lessThan(lhs.value))
}
}
這樣,您可以將sort方法簽名撰寫為:
mutating func sort(by criteria: (path: KeyPath<Element, AnyComparable>, order:OrderType)...) {
}
為了讓我們更容易傳遞帶有型別的關鍵路徑AnyComparable,我們可以做一個擴展:
extension Comparable {
// this name might be too long, but I'm sure you can come up with a better name
var anyComparable: AnyComparable {
.init(self)
}
}
現在我們可以這樣做:
someArray.sort(by: (\.key1.anyComparable, .asc), (\.key2.anyComparable, .asc))
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/405056.html
標籤:
下一篇:提高此過濾功能的效率
