NSPredicate with Swift and Core Data

前端 未结 3 1733
星月不相逢
星月不相逢 2021-02-02 13:29

i have a Core Data Object and i have 2 Fieds (one String(GUID) and one Int which i want to use as Filter)

So in SQL it would be \"SELECT * FROM Answers WHERE qIndex = 1

相关标签:
3条回答
  • 2021-02-02 13:40

    Instead of worrying about %@ conversions and then composing AND predicates, you can use the PredicatePal framework:

    let compound = *(Key("qIndex") == qIndex && Key("formUUID") == formUUID)
    

    Assuming that qIndex and formUUID are the correct type, Swift will automatically deduce the correct types for the Key objects.

    0 讨论(0)
  • 2021-02-02 13:46

    This is not the exact response to your question, but a problem people might now encouter with your code now:

    In the latest version of XCode, you must now unwrap the predicate, like this:

    var compound = NSCompoundPredicate.andPredicateWithSubpredicates([predicate1!, predicate2!])
    

    because NSPredicate initializer now return NSPredicate? type.

    0 讨论(0)
  • 2021-02-02 13:52

    If formUUID is an NSString or a Swift String then you have to use the %@ placeholder:

    let resultPredicate2 = NSPredicate(format: "formUUID = %@", formUUID)
    
    0 讨论(0)
提交回复
热议问题