How do I add a variable to a fetch request in core data in Xcode 4? Can't find it.
Fetch requests allow us to load Core Data results that match specific criteria we specify, and SwiftUI can bind those results directly to user interface elements. If you followed my Core Data and SwiftUI set up instructions, you've already injected your managed object context into the SwiftUI environment.
Fetching Data From CoreData We have created a function fetch() whose return type is array of College(Entity). For fetching the data we just write context. fetch and pass fetchRequest that will generate an exception so we handle it by writing try catch, so we fetched our all the data from CoreData.
A description of search criteria used to retrieve data from a persistent store.
Still inside the Core Data editor, go to the Editor menu and choose Create NSManagedObject Subclass. Make sure your data model is selected then click Next. Make sure the Commit entity is checked then click Next again.
Select Expression
from the drop down and enter the expression using a $
before your substitution variable (NAME
in the example below). Even if the substitution variable's value is a string, make sure you don't put the variable between quotes, or the substitution will not work.
In your code you refer to the fetch predicate like this (XCode 4.4 and above):
NSManagedObjectModel* model = [[context persistentStoreCoordinator] managedObjectModel];
NSFetchRequest* request = [model fetchRequestFromTemplateWithName:templateName
substitutionVariables:@{@"NAME" : name}];
NSError* error = nil;
NSArray* results = [context executeFetchRequest:request error:&error];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With