Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/199.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift中NSFETCH请求的多个NSPredicates?_Swift_Nspredicate - Fatal编程技术网

Swift中NSFETCH请求的多个NSPredicates?

Swift中NSFETCH请求的多个NSPredicates?,swift,nspredicate,Swift,Nspredicate,目前,我有一个简单的NSFetchRequest和一个关联的NSPredicate。然而,我希望有一种方法可以附加多个谓词。我在Objective C中看到过一些例子,但在Swift中没有 您是否可以定义NSPredicate的列表或以某种方式将多个NSPredicate对象附加到单个NSFetchRequest 谢谢 您可以使用“NSCompoundPredicate”。例如: let converstationKeyPredicate = NSPredicate(format: "conve

目前,我有一个简单的NSFetchRequest和一个关联的NSPredicate。然而,我希望有一种方法可以附加多个谓词。我在Objective C中看到过一些例子,但在Swift中没有

您是否可以定义NSPredicate的列表或以某种方式将多个NSPredicate对象附加到单个NSFetchRequest

谢谢

您可以使用“NSCompoundPredicate”。例如:

let converstationKeyPredicate = NSPredicate(format: "conversationKey = %@", conversationKey)
let messageKeyPredicate = NSPredicate(format: "messageKey = %@", messageKey)
let andPredicate = NSCompoundPredicate(type: NSCompoundPredicateType.AndPredicateType, subpredicates: [converstationKeyPredicate, messageKeyPredicate])
request.predicate = andPredicate

您可以更改为“AndPredicateType”或“OrPredicateType”

更新Swift 4

let predicateIsNumber = NSPredicate(format: "isStringOrNumber == %@", NSNumber(value: false))
let predicateIsEnabled = NSPredicate(format: "isEnabled == %@", NSNumber(value: true))
let andPredicate = NSCompoundPredicate(type: .and, subpredicates: [predicateIsNumber, predicateIsEnabled])

//check here for the sender of the message

let fetchRequestSender = NSFetchRequest<NSFetchRequestResult>(entityName: "Keyword")
fetchRequestSender.predicate = andPredicate
希望对你有帮助


感谢

API是相同的,因此您看到的示例是有效的-如果您有问题,请尝试并显示代码,但是如果本示例中的messageKeyPredicate仅在某些情况下存在,该怎么办。在尝试构造NSCompoundPredicate时,有没有一种优雅的方法可以避免使用spagetti代码?不管怎样,只需构建NSPredicate列表就可以了!非常感谢。我们可以使用andPredicate吗?或者有什么区别?让andPredicate=NSCompoundPredicate(andPredicateWithSubpredicates:[PredicateNumber,predicateIsEnabled])我可以有两种谓词吗,一种是带有
、和
条件的谓词,另一种是带有
、或
条件的谓词?我想可以将其封装在另一个或复合谓词中
`NSCompoundPredicateType.AndPredicateType` replaced by `NSCompoundPredicate.LogicalType.and`