Home Ask Login Register

Developers Planet

Your answer is one click away!

Zygote February 2016

Multiple NSPredicates for NSFetchRequest in Swift?

Currently, I have a simple NSFetchRequest with an associated NSPredicate. However, Im hoping there is a way you can append multiple predicates. I've seen examples in Objective C, but none for Swift.

Can you define a list of NSPredicate's or append multiple NSPredicate objects to a single NSFetchRequest somehow?



Manuel February 2016

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"

Post Status

Asked in February 2016
Viewed 2,187 times
Voted 13
Answered 1 times


Leave an answer

Quote of the day: live life