accept NSManagedObject as Where predicate argument

This commit is contained in:
John Rommel Estropia
2017-04-01 17:15:24 +09:00
parent ecb3d0cfa0
commit 1ad6ac5769

View File

@@ -133,6 +133,25 @@ public struct Where: FetchClause, QueryClause, DeleteClause, Hashable {
}
}
/**
Initializes a `Where` clause that compares equality
- parameter keyPath: the keyPath to compare with
- parameter object: the arguments for the `==` operator
*/
public init<T: NSManagedObject>(_ keyPath: KeyPath, isEqualTo object: T?) {
switch object {
case nil,
is NSNull:
self.init(NSPredicate(format: "\(keyPath) == nil"))
case let object?:
self.init(NSPredicate(format: "\(keyPath) == %@", argumentArray: [object.objectID]))
}
}
/**
Initializes a `Where` clause that compares membership
@@ -144,6 +163,17 @@ public struct Where: FetchClause, QueryClause, DeleteClause, Hashable {
self.init(NSPredicate(format: "\(keyPath) IN %@", list.map({ $0.cs_toQueryableNativeType() }) as NSArray))
}
/**
Initializes a `Where` clause that compares membership
- parameter keyPath: the keyPath to compare with
- parameter list: the sequence to check membership of
*/
public init<S: Sequence>(_ keyPath: KeyPath, isMemberOf list: S) where S.Iterator.Element: NSManagedObject {
self.init(NSPredicate(format: "\(keyPath) IN %@", list.map({ $0.objectID }) as NSArray))
}
/**
Initializes a `Where` clause with an `NSPredicate`