NSPredicate with Swift and Core Data
If formUUID
is an NSString
or a Swift String
then you have to use the%@
placeholder:
let resultPredicate2 = NSPredicate(format: "formUUID = %@", formUUID)
Filter Vs NSPredicate in coredata
From Persistent Store Types and Behaviors in the Core Data Programming Guide (emphasis added):
Fetching differs somewhat according to the type of store. In the XML, binary, and in-memory stores, evaluation of the predicate and sort descriptors is performed in Objective-C with access to all Cocoa functionality, including the comparison methods on NSString.
The SQLite store, on the other hand, compiles the predicate and sort descriptors to SQL and evaluates the result in the database itself. This is done primarily for performance, ...
You can verify that by enabling Core Data debugging. Set
-com.apple.CoreData.SQLDebug 3
-com.apple.CoreData.Logging.stderr 1
as environment variables and you'll see the SQLite statements as they are executed.
core data how to filter (NSPredicate) including a relationship requirement and given the relationship object?
The predicate would be what you expect.
NSPredicate(format: "name = %@ && school = %@", "Tom", school)
However, you can get to the person without a predicate by using the relationship in the other direction and filter.
let tom = school.persons.filter { $0.name == "Tom" }.first
(You might have to cast your NSSet
to Set<Person>
).
Can I use an NSPredicate in Swift with a nil argument?
It looks as if it is (with the current Xcode 6 beta 3 release) generally difficult
to pass nil
in a variable argument list.
This seems to work:
let predicate = NSPredicate(format: "parentFolder == %@", 0)
print(predicate)
// Output: parentFolder == nil
But the easiest solution would be to simply write the predicate as
NSPredicate(format: "parentFolder == nil")
Swift Core Data Predicate IN Clause
You don't show how you are defining recentEmployeeIds
, but assuming its something like
let recentEmployeeIds:[Int] = ...
then you need to init the NSPredicate
correctly. There is no label for argumentArray
fetchRequest.predicate = NSPredicate(format: "ANY id IN %@", recentEmployeeIds)
Core Data NSPredicate - Match Dates
First of all the exception occurs because the placeholder %@
is wrong. %@
is for objects, a Double
value is represented by %f
.
Second of all if you want to fetch all dates which are in a specific day you have to create a date range (0:00–23:59) of that day.
Using Calendar
you get 0:00 with startOfDay(for:)
and the end of the day by adding a day
and the <
operator.
For example
let calendar = Calendar.current
let startDate = calendar.startOfDay(for: selectedDate)
let endDate = calendar.date(byAdding: .day, value: 1, to: startDate)!
let predicate = NSPredicate(format: "date >= %@ AND date < %@", argumentArray: [startDate, endDate]))
Swift 5 CoreData predicate using UUID
- The
%K
format expects a key path string, this can be obtained using the#keyPath
directive. - The
%@
format expects an Objective-C object, here we can use the toll-free bridging betweenUUID
andNSUUID
.
That gives:
let predicate = NSPredicate(format: "%K == %@",
#keyPath(Bike.bleID), bleID as NSUUID)
Related Topics
Convert String to Cgfloat in Swift
Show/Hide Password - How to Add This Feature
Switch That Checks Nsindexpath's Row and Section
Uiimage Created from Mtkview Results in Color/Opacity Differences
Checking If a Swift Class Conforms to a Protocol and Implements an Optional Function
How to Create a Hud on Top of My Scenekit.Scene
Rxswift: Observable While a Button Holds Down
How to Remove Multiple Items from a Swift Array
Swift Combine Alternative to Rx Observable.Create
Value for Swift_Version Cannot Be Empty
How to Enable Swift Support for Existing Project in Flutter
One-Way Platform Collisions in Sprite Kit
Local Swift Packages Stopped Working in Xcode 13
Add Text Label and Button to Dynamic Tableview Cell Programmatically with Swift
In Swift, When Using "[Weak Self] In", Should I Double Up on It When Nested Inside Another Closure