任何人都可以告诉我如何为 Objective C 中具有 NSDictionary 的 NSArray 设置 NSPredicate 吗?

Can any one tell me how to set NSPredicate for NSArray having NSDictionary in Objective C?

我的结构为:

[
 {
  "time": {
    "td": 3036
  },
   "creditCardType": {
     "cardType": "MasterCard",
     "default": false
   },
   "transactionid": {
     "transactionReferenceNo": "xyz",
     "amount": 11.62,
     "transactionStatus": "SUCCESS"
   }
 },
 {
   "time": {
     "td": 3037
   },
   "creditCardType": {
     "cardType": "MasterCard",
     "default": false
   },
   "transactionid": {
     "transactionReferenceNo": "xyp",
     "amount": 13.62,
     "transactionStatus": "SUCCESS"
   }
  }
]

在此我必须删除关于 transactionReferenceNo 的重复项,因为我将 Predicate 设置为:

NSDictionary *transactionDict = [dict valueForKey:@"transactionid"];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"ANY %K.%K CONTAINS[c] %@", @"transactionid", @"transactionReferenceNo",[transactionDict valueForKey:@"transactionReferenceNo"]];`

这会使我的应用程序崩溃并出现错误:

The left hand side for an ALL or ANY operator must be either an NSArray or an NSSet.

我做错了什么..

提前致谢。

试试这个

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"transactionid %K.%K CONTAINS[c] %@", @"transactionid", @"transactionReferenceNo",[transactionDict valueForKey:@"transactionReferenceNo"]];
NSArray *arr = @[
                 @{
                     @"time": @{
                         @"td": @3036
                     },
                     @"creditCardType": @{
                         @"cardType": @"MasterCard",
                         @"default": @NO
                     },
                     @"transactionid": @{
                         @"transactionReferenceNo": @"xyz",
                         @"amount": @(11.62),
                         @"transactionStatus": @"SUCCESS"
                     }
                 },
                 @{
                     @"time": @{
                         @"td": @3037
                     },
                     @"creditCardType": @{
                         @"cardType": @"MasterCard",
                         @"default": @NO
                     },
                     @"transactionid": @{
                         @"transactionReferenceNo": @"xyp",
                         @"amount": @(13.62),
                         @"transactionStatus": @"SUCCESS"
                     }
                 }
                 ];

NSPredicate *predicate = [NSPredicate predicateWithFormat:
                          @"transactionid.transactionReferenceNo CONTAINS[c] %@",
                          @"xyp"];

NSArray *arr2 = [arr filteredArrayUsingPredicate:predicate];
NSLog(@"arr2 = %@", arr2);

在你的例子中,谓词应该是这样的:

NSPredicate *predicate = [NSPredicate predicateWithFormat:
                          @"transactionid.transactionReferenceNo CONTAINS[c] %@",
                          transactionDict[@"transactionReferenceNo"]];