Adding two NSMutableDictionaries in 1 NSMutableArray

101 Views Asked by At

Here,I am trying to get both the followers and followings of twitter using FHSTwitterEngine. Here is my code to get followers and adding dictionary into NSMutableArray to print in a tableView.

NSMutableDictionary *dict1 = [[FHSTwitterEngine sharedEngine]listFollowersForUser:usernames isID:NO withCursor:@"-1"];
  NSLog(@"====> %@",[dict1 objectForKey:@"users"] );
 twttrFrndListVC.twitterFrndList=[dict1 objectForKey:@"users"];

Using above code,I can get followers. But when i try to get both followers and followings,it doesn't work.

 NSMutableDictionary *dict1 = [[FHSTwitterEngine sharedEngine]listFollowersForUser:username isID:NO withCursor:@"-1"];
 NSMutableDictionary *dict2 = [[FHSTwitterEngine sharedEngine] listFriendsForUser:username isID:NO withCursor:@"-1"];
      NSLog(@"====> %@",[dict1 objectForKey:@"users"] );
     twttrFrndListVC.twitterFrndList=[dict1 objectForKey:@"users"],[dict2 objectForKey:@"users"];

Above code returns followers name only. How can I get values of both dictionaries ? How can I add two NSMutableDictionaries in one NSMutableArray ? Both the dictionaries have same keys.

1

There are 1 best solutions below

2
On BEST ANSWER
  1. You are only printing out dict1, that might lead to the assumption that you only get the followers back
  2. I guess twttrFrndListVC.twitterFrndList is an NSArray.

To union different arrays together you want to use the following code:

NSArray *arr1 = [[NSArray alloc]init];
NSArray *arr2 = [[NSArray alloc]init];

NSArray *arr1ANDarr2 = [[NSArray alloc]initWithArray:arr1];
arr1ANDarr2 = [arr1ANDarr2 arrayByAddingObjectsFromArray:arr2];

In your specific case that would be:

twttrFrndListVC.twitterFrndList = [[NSArray alloc]initWithArray:[dict1 objectForKey:@"users"]];
twttrFrndListVC.twitterFrndList = [twttrFrndListVC.twitterFrndList arrayByAddingObjectsFromArray:[dict2 objectForKey:@"users"]];

Unfortunately I cannot tell if your Twitter code is correct as I've never worked with that. Hope that helps!

EDIT: I just saw you are using an NSMutableArray, which adds an additional way to do this:

NSArray *arr1 = [[NSArray alloc]init];
NSArray *arr2 = [[NSArray alloc]init];

NSMutableArray *arr1ANDarr2 = [[NSMutableArray alloc]initWithArray:arr1];
[arr1ANDarr2 addObjectsFromArray:arr2]; 

In your case:

twttrFrndListVC.twitterFrndList = [[NSMutableArray alloc]initWithArray:[dict1 objectForKey:@"users"]];
[twttrFrndListVC.twitterFrndList addObjectsFromArray:[dict2 objectForKey:@"users"]]];