-
Notifications
You must be signed in to change notification settings - Fork 4
/
CustomSortDescriptorConvertible.swift
41 lines (35 loc) · 1.42 KB
/
CustomSortDescriptorConvertible.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
//
// CustomSortDescriptorConvertible.swift
// CoreDataQueryInterface
//
// Created by Gregory Higley on 6/14/15.
// Copyright © 2015 Prosumma LLC. All rights reserved.
//
import Foundation
/**
`CustomSortDescriptorConvertible` represents a type that can be converted to a sort descriptor.
- note: `NSSortDescriptor` itself implements this protocol. It ignores the
`ascending` parameter of `toSortDescriptor`. So, `order(descending: NSSortDescriptor(key: "foo", ascending: true))`
sorts ascending, *not* descending. For this reason, it's best to use
`NSSortDescriptor` with the overloads of `order()` that do not contain the
`descending` keyword argument.
*/
public protocol CustomSortDescriptorConvertible {
/** Convert the underlying type to `NSSortDescriptor` */
func toSortDescriptor(ascending ascending: Bool) -> NSSortDescriptor
}
extension NSSortDescriptor: CustomSortDescriptorConvertible {
public func toSortDescriptor(ascending ascending: Bool) -> NSSortDescriptor {
return self
}
}
extension String: CustomSortDescriptorConvertible {
public func toSortDescriptor(ascending ascending: Bool) -> NSSortDescriptor {
return NSSortDescriptor(key: self, ascending: ascending)
}
}
extension Attribute: CustomSortDescriptorConvertible {
public func toSortDescriptor(ascending ascending: Bool) -> NSSortDescriptor {
return NSSortDescriptor(key: String(self), ascending: ascending)
}
}