nspredicate
How to find beginswith word in a string using NSPredicate?
Simply use BEGINSWITH instead of CONTAINS. Edit If you need to search in every word of a string, there is a technique which was presented in one of the talks in WWDC 2010. The basic idea is to create a separate entity Word which contains a single word and a reference of the containing object … Read more
Combining Two Conditions in NSPredicate
As already said, you can use logical operators like “AND”, “OR” in predicates. Details can be found in Predicate Format String Syntax in the “Predicate Programming Guide”. As an alternative, use “compound predicates”: let p1 = NSPredicate(format: “username = %@”, “user”) let p2 = NSPredicate(format: “password = %@”, “password”) let predicate = NSCompoundPredicate(andPredicateWithSubpredicates: [p1, p2]) … Read more
Can’t pass Date to NSPredicate(format: …) without “as CVarArg”
The %@ format expect a Foundation object as argument, compare “Predicate Format String Syntax” in the “Predicate Programming Guide”. Therefore you have to cast the overlay type Date back to its Foundation counterpart NSDate: let endDate = Date() let pred = NSPredicate(format: “endDate == %@”, endDate as NSDate)
Using NSPredicate to determine if a string equals another string
Try [NSPredicate predicateWithFormat:@”title ==[c] ‘on call'”]; (The [c] makes the equality comparison case-insensitive.)
Can I apply multiple predicates to an NSFetchRequest? Would it be better to manually parse my results?
Yes it’s possible. You’re looking for compound predicates and here’s an example with AND predicates: NSPredicate *compoundPredicate = [NSCompoundPredicate andPredicateWithSubpredicates:[NSArray of Predicates]]; You can also use notPredicateWithSubpredicates and orPredicateWithSubpredicates depending on your needs. Link to documentation https://developer.apple.com/documentation/foundation/nscompoundpredicate
Multiple NSPredicates for NSFetchRequest in Swift?
You can use “NSCompoundPredicate”. For example: let converstationKeyPredicate = NSPredicate(format: “conversationKey = %@”, conversationKey) let messageKeyPredicate = NSPredicate(format: “messageKey = %@”, messageKey) let andPredicate = NSCompoundPredicate(type: NSCompoundPredicateType.AndPredicateType, subpredicates: [converstationKeyPredicate, messageKeyPredicate]) request.predicate = andPredicate You can change into “AndPredicateType” or “OrPredicateType”
SwiftUI View and @FetchRequest predicate with variable that can change
had the same problem, and a comment of Brad Dillon showed the solution: var predicate:String var wordsRequest : FetchRequest<Word> var words : FetchedResults<Word>{wordsRequest.wrappedValue} init(predicate:String){ self.predicate = predicate self.wordsRequest = FetchRequest(entity: Word.entity(), sortDescriptors: [], predicate: NSPredicate(format: “%K == %@”, #keyPath(Word.character),predicate)) } in this example, you can modify the predicate in the initializer.
NSFetchedResultsController: changing predicate not working?
I had almost exactly this problem, until I found the hint in a very recent blog post at iphone incubator NSFetchedResultsController is caching the first results. (You probably have something set at initWithFetchRequest:managedObjectContext:sectionNameKeyPath:cacheName) I’m guessing your code (like mine) is a derivation of the CoreData sample, so assuming it’s @”Root”, before you change the predicate, … Read more
Unable to query for date using NSPredicate in Swift
In swift 3 the predicate changed to: let datePredicate = NSPredicate(format: “date > %@”, currentDate as NSDate)