【问题标题】:Create and sort array of dictionaries from previous array of dictionaries从以前的字典数组创建和排序字典数组
【发布时间】: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++;
}

所以逻辑是我正在检查我的名字在哪里(userfollowing)。然后我正在检查这个人是否已经是followingMe。它是_allfriends 中的一个条目,我需要获取该索引并且我想更新“following”...有意义吗?我在这里做错了什么?

【问题讨论】:

    标签: ios arrays dictionary


    【解决方案1】:

    你可以用 Set 对数组进行排序

    SortedArr = [NSMutableArray arrayWithArray:[[NSSet setWithArray: UnsortedArr] allObjects]];
    

    【讨论】:

    • 我不确定这对我有什么帮助,但它只是给了我原始数组
    • 抱歉,但这会从数组中删除重复元素...不排序
    【解决方案2】:

    显然问题出在NSMutableDictionary *temp = [NSMutableDictionary new]; 上。我不确定this post 是否适用,但它让我重写了我的代码。 temp 值附加到 _allfriends 数组,所以当 temp 改变时,_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];
            }
        }
    }
    

    希望对某人有所帮助...

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-09-28
      • 2013-11-28
      • 2020-01-18
      • 1970-01-01
      • 2017-06-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多