PiyushRathi
PiyushRathi

Reputation: 959

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

I have Structure as :

[
 {
  "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"
   }
  }
]

in this i have to remove duplicates with respect to transactionReferenceNo, for that i set Predicate is as:

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

this will crashes my app and gives me error:

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

what wrong m doing..

Thanks in advance.

Upvotes: 1

Views: 72

Answers (2)

Michael Kessler
Michael Kessler

Reputation: 14235

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);

In your case the predicate should be like this:

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

Upvotes: 1

Mahendra
Mahendra

Reputation: 8914

Try this

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

Upvotes: 0

Related Questions