struct KeyPathComparator<Compared> : SortComparator

<aside> πŸ’‘ λ‹€λ₯Έ μ •λ ¬ μ•Œκ³ λ¦¬μ¦˜μ— keypath의 값을 μ „λ‹¬ν•˜μ—¬ λΉ„κ΅ν•˜λŠ” μ •λ ¬ μ•Œκ³ λ¦¬μ¦˜.

</aside>

κ°œμš”

KeyPathComparatorλŠ” μ •λ ¬ μ•Œκ³ λ¦¬μ¦˜μ„ 생성 및 μ „λ‹¬ν•˜λŠ” μ‰¬μš΄ 방법을 μ œκ³΅ν•œλ‹€. 비ꡐ할 νƒ€μž…μ—μ„œ 비ꡐ λŒ€μƒ ν”„λ‘œνΌν‹°λ₯Ό KeyPath둜 λͺ…μ‹œν•˜κ³ , λ‹€λ₯Έ Comparator에 ν•΄λ‹Ή ν”„λ‘œνΌν‹°μ˜ 비ꡐ λ™μž‘μ„ μœ„μž„ν•œλ‹€. μƒμ„±μžμ—μ„œ comparator 인자λ₯Ό λͺ…μ‹œν•˜μ§€ μ•Šμ„ 경우 기본적으둜 ComparableComparatorλ₯Ό μ •λ ¬ μ•Œκ³ λ¦¬μ¦˜μœΌλ‘œ μ‚¬μš©ν•œλ‹€.

SortDescriptorμ™€μ˜ κ°€μž₯ 큰 차이점은 μ •λ ¬ λŒ€μƒ 객체가 Obj-C λŸ°νƒ€μž„μ— λ…ΈμΆœλ  ν•„μš”κ°€ μ—†λ‹€λŠ” 것이닀. 즉 객체가 NSObjectλ₯Ό 상속받아야 ν•œλ‹€κ±°λ‚˜ ν”„λ‘œνΌν‹°μ— @objc νŠΉμ„±μ„ λΆ™μ—¬μ•Όν•˜λŠ” μ œμ•½μ΄ μ—†λ‹€.

SortComparator의 μ‚¬μš©μ²˜μ—μ„œ μ‚¬μš©λœλ‹€.

API

생성

ν”„λ‘œν† μ½œ μ€€μˆ˜

λͺ¨μ•„보기

init<Value>(_ keyPath: KeyPath<Compared, Value>, order: SortOrder = .forward) where Value : Comparable
init<Value>(_ keyPath: KeyPath<Compared, Value?>, order: SortOrder = .forward) where Value : Comparable
init<Value, Comparator>(_ keyPath: KeyPath<Compared, Value>, comparator: Comparator) where Value == Comparator.Compared, Comparator : SortComparator
init<Value, Comparator>(_ keyPath: KeyPath<Compared, Value?>, comparator: Comparator) where Value == Comparator.Compared, Comparator : SortComparator
init<Value, Comparator>(_ keyPath: KeyPath<Compared, Value>, comparator: Comparator, order: SortOrder) where Value == Comparator.Compared, Comparator : SortComparator
init<Value, Comparator>(_ keyPath: KeyPath<Compared, Value?>, comparator: Comparator, order: SortOrder) where Value == Comparator.Compared, Comparator : SortComparator

// conforming SortComparator
compare(_ lhs: Compared, _ rhs: Compared) -> ComparisonResult
var order: SortOrder
**let keyPath: PartialKeyPath<Compared>**
    
// SortComparator inherits Hashable
func hash(into hasher: inout Hasher)
var hashValue: Int
    
// Hashable inherits Equatable
static func == (lhs: KeyPathComparator<Compared>, rhs: KeyPathComparator<Compared>) -> Bool
static func != (lhs: KeyPathComparator<Compared>, rhs: KeyPathComparator<Compared>) -> Bool

참고자료