ios - 谁能告诉我如何在 Objective C 中为具有 NSDictionary 的 NSArray 设置 NSPredicate?

标签 ios objective-c iphone ipad nspredicate

我的结构为:

[
 {
  "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.

我做错了什么..

提前致谢。

最佳答案

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"]];

关于ios - 谁能告诉我如何在 Objective C 中为具有 NSDictionary 的 NSArray 设置 NSPredicate?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36263575/

相关文章:

iphone - 在适用于 iOS 和非公共(public) API 的 Phonegap 应用程序中使用 Jquery Mobile

ios - UIAlertController——在该 block 中强捕获 'controller'

ios - 将任务分派(dispatch)到队列中以便在另一个线程中运行任务

ios - 什么时候创建 Storyboard中的元素?

ios - 使用带有随机化字符串的 UILocalNotification

ios - 如何根据 TextField 的可见大小限制 UITextField 的输入文本量?

iphone - glGenLists 和 iOS

cocoa-touch - iOS indexPath 为零(自定义附件图像)

objective-c - 声明 UITouchGestures?

iphone - 如何以编程方式缩放网页 View ?