NSPredicate 获取属性不在给定数组中的实体

NSPredicate get entities with attribute not in a given array

我正在使用 CloudKit 在我的应用程序中存储和同步一些数据。假设我有一个记录类型,其属性名为 key。我想获取所有 key 不在给定数组 unwantedKeyArray.

中的记录

我试过了

let predicate = NSPredicate(format: "key NOT IN %@", unwantedKeyArray)

但是应用程序崩溃并显示错误

'Unable to parse the format string "key NOT IN %@"'

那么创建 NSPredicate 以实现我的目标的正确方法是什么?

(虽然我在我的应用程序和这个问题描述中使用 Swift,但也欢迎使用 ObjC 回答)

试试这个代码示例:

   NSPredicate(format: "NOT (key IN %@)", unwantedKeyArray)