2014-06-06 63 views
0

我真的試着不要在這裏問這個問題的前一陣,但嘗試小時後排序字典的數組,我卡住了。所以這裏...希望我可以清楚。創建並從字典

我所需的輸出是這樣的:

({ 
    following = ""; 
    followingMe = ""; 
},{ 
    following = ""; 
    followingMe = ""; 
},{...}) 

在這個樣子的,我想做什麼數據來排序是它

({ 
    user = "me"; 
    following = "sam"; 
},{ 
    user = "sam"; 
    following = "me"; 
},{ 
    user = "foo"; 
    following = "me"; 
},{ 
    user = "me"; 
    following = "bar"; 
},...) 

我想組織這麼如果我跟着「山姆」,他跟着我,這是一本字典。如果「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」 ......是有道理的?我在做什麼錯在這裏?

回答

0

U可以與集

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

我不知道應當如何幫助我,但它只是給了我原來的陣列右後衛 – denikov

+0

對不起,但這會從數組中刪除重複的元素...不排序 –

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]; 
     } 
    } 
} 

希望幫助別人......