【发布时间】:2014-06-06 10:04:44
【问题描述】:
我真的试着不在这里问这个问题,但经过几个小时的尝试,我被困住了。所以这里......希望我能清楚。
我想要的输出是这样的:
({
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 中的一个条目,我需要获取该索引并且我想更新“following”...有意义吗?我在这里做错了什么?
【问题讨论】:
标签: ios arrays dictionary