gpt4 book ai didi

ios distinctUnionOfObjects 没有返回字典的所有内容

转载 作者:可可西里 更新时间:2023-11-01 05:55:35 24 4
gpt4 key购买 nike

我有一个带有 NSDictionaryNSArray 并尝试使用以下代码删除重复项:

NSDictionary *arnold = @{@"name" : @"arnold", @"state" : @"california"};
NSDictionary *jimmy = @{@"name" : @"jimmy", @"state" : @"new york"};
NSDictionary *henry = @{@"name" : @"henry", @"state" : @"michigan"};
NSDictionary *woz = @{@"name" : @"woz", @"state" : @"california"};

NSArray *people = @[arnold, jimmy, henry, woz];

NSMutableArray *results=[[NSMutableArray alloc]initWithArray: [people valueForKeyPath:@"@distinctUnionOfObjects.state"]];
NSLog(@"results %@", results);

这是我从 nslog 得到的输出:

results (
california,
michigan,
"new york"
)

我的问题是如何将完整的目录添加到数组中?

最佳答案

@distinctUnionOfObjects 将只返回指定该属性的唯一对象值,而不是对象本身。

你可以试试这个:

NSDictionary *arnold = @{@"name" : @"arnold", @"state" : @"california"};
NSDictionary *jimmy = @{@"name" : @"jimmy", @"state" : @"new york"};
NSDictionary *henry = @{@"name" : @"henry", @"state" : @"michigan"};
NSDictionary *woz = @{@"name" : @"woz", @"state" : @"california"};

NSArray *people = @[arnold, jimmy, henry, woz];

NSMutableArray *uniqueArray = [[NSMutableArray alloc] init];
NSMutableSet *checkedStates = [[NSMutableSet alloc] init];

for (NSDictionary *person in people) {

NSString *currentStateName = person[@"state"];
BOOL isDuplicateState = [checkedStates containsObject:currentStateName];

if (!isDuplicateState) {
[uniqueArray addObject:person];
[checkedStates addObject:currentStateName];
}
}

NSLog(@"Results %@", uniqueArray);

NSLog 的输出将是:

Results (
{
name = arnold;
state = california;
},
{
name = jimmy;
state = "new york";
},
{
name = henry;
state = michigan;
}
)

关于ios distinctUnionOfObjects 没有返回字典的所有内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17433172/

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