gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 12:02:48 25 4
gpt4 key购买 nike

我的结构为:

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com