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)

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