我的结构为:
[
{
"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/