我真的试着不在这里问这个问题,但经过几个小时的努力,我被困住了。所以这里...希望我能清楚。
我想要的输出是:
({
following = "";
followingMe = "";
},{
following = "";
followingMe = "";
},{...})
进入的数据看起来像这样,我想要做的就是排序
({
user = "me";
following = "sam";
},{
user = "sam";
following = "me";
},{
user = "foo";
following = "me";
},{
user = "me";
following = "bar";
},...)
我想组织它,如果我跟随" sam"而且他跟着我,这是一本字典。如果" foo"跟着我,然后following="(null)
"和followingMe="sam"
......等等。这就是为什么我不想问这个问题......很难解释。
这里是我的代码,但它只是给我一遍又一遍的条目......那是什么错字?
NSMutableDictionary *temp = [NSMutableDictionary new];
int i = 0;
while (i < array.count) {
[temp removeAllObjects];
if ([[[array objectAtIndex:i]valueForKey:@"user"]isEqualToString:_user]){
//means I'm following this person
[temp setValue:[[array objectAtIndex:i] valueForKey:@"following"] forKey:@"following"];
if ([[_allfriends valueForKey:@"followingMe"]containsObject:[[array objectAtIndex:i] valueForKey:@"following"]]) {
//check to see if this person is already following me
[[_allfriends objectAtIndex:[[_allfriends valueForKey:@"followingMe"] indexOfObject:
[[array objectAtIndex:i] valueForKey:@"following"]]] setObject:[[array objectAtIndex:i] valueForKey:@"following"] forKey:@"following"];
//trying to update at the index where this person is following me
}else{
[temp setObject:@"(null)" forKey:@"followingMe"];
[_allfriends addObject:temp];
}
}else{
//this person is following me
[temp setObject:[[array objectAtIndex:i]valueForKey:@"user"] forKey:@"followingMe"];
if ([[_allfriends valueForKey:@"following"]containsObject:[[array objectAtIndex:i] valueForKey:@"user"]]) {
//check to see if I'm following this pseron
[[_allfriends objectAtIndex:[[_allfriends valueForKey:@"following"] indexOfObject:
[[array objectAtIndex:i] valueForKey:@"user"]]] setObject:[[array objectAtIndex:i] valueForKey:@"user"] forKey:@"followingMe"];
//try to update followingMe at that index
}else{
[temp setObject:@"(null)" forKey:@"following"];
[_allfriends addObject:temp];
}
}
i++;
}
所以我的逻辑是检查我的姓名(user
或following
)。然后我会检查此人是否已经followingMe
。它是_allfriends
中的一个条目,我需要获取该索引,我想更新&#34; following
&#34; ...有意义吗?我在这里做错了什么?
答案 0 :(得分:0)
U可以使用Set
对数组进行排序SortedArr = [NSMutableArray arrayWithArray:[[NSSet setWithArray: UnsortedArr] allObjects]];
答案 1 :(得分:0)
显然问题出在NSMutableDictionary *temp = [NSMutableDictionary new];
上。我不确定this post是否适用,但它让我重写了我的代码。临时值附加到_allfriends
数组,因此当临时更改时,_allfriends
中的所有内容都会更改。我的循环逻辑是正确的,但我重写了这样:
NSString *key = [NSString new];
for (int i = 0; i < array.count; i++) {
if ([[[array objectAtIndex:i]valueForKey:@"user"]isEqualToString:_user]){
key = [[array objectAtIndex:i] valueForKey:@"following"];
if ([[_allfriends valueForKey:@"followingMe"]containsObject:key]) {
[_allfriends removeObjectAtIndex:[[_allfriends valueForKey:@"followingMe"] indexOfObject:key]];
[_allfriends insertObject:[NSDictionary dictionaryWithObjectsAndKeys:key, @"following", key, @"followingMe", nil] atIndex:_allfriends.count];
}else{
[_allfriends insertObject:[NSDictionary dictionaryWithObjectsAndKeys:key, @"following", @"(null)", @"followingMe", nil] atIndex:_allfriends.count];
}
}else{
key = [[array objectAtIndex:i] valueForKey:@"user"];
if ([[_allfriends valueForKey:@"following"]containsObject:key]) {
[_allfriends removeObjectAtIndex:[[_allfriends valueForKey:@"following"] indexOfObject:key]];
[_allfriends insertObject:[NSDictionary dictionaryWithObjectsAndKeys:key, @"following", key, @"followingMe", nil] atIndex:_allfriends.count];
}else{
[_allfriends insertObject:[NSDictionary dictionaryWithObjectsAndKeys:key, @"followingMe", @"(null)", @"following", nil] atIndex:_allfriends.count];
}
}
}
希望有人帮助......