0

I'm building an "invite friends" feature. It's already working I just have one issue I'm wrestling with. I'm retrieving my contact list, and every time I select a contact I'm adding them to a NSMutableArray which I'm calling "selectedUser".

So each item in the NSMutableArray at this point are "Dictionaries" and some of the values are "Dictionaries" as well. Especially the "phones" key I'm trying to access and retrieve the value key.

What I'm trying to accomplish is to only retrieve the "phone numbers" in strings stored them inside a NSArray that I can then past to [messageController setRecipients:recipents]; recipents being the array of only NSStrings of phone numbers.

This is my code so far, and what I'm getting is a NSArray with multiple NSArrays in it were each array only has one string being the phone number.

  NSArray *titles = [self.selectedUsers valueForKey:@"phones"];
  NSArray *value = [titles valueForKey:@"value"];
  NSLog(@"Output the value: %@", value);
  NSArray *recipents = value;

This is what I get in the log

2016-01-04 12:27:59.721 InviteFriends[4038:1249174] (
        (
        "(305) 731-7353"
    ),
        (
        "(786) 306-2831"
    ),
        (
        "(305) 333-3297"
    )
)

This is the log of the dictionary itself

    {
    birthday = "";
    company = "";
    createdAt = "2015-09-06 16:14:18 +0000";
    department = "";
    emails =         (
    );
    firstName = "Lola";
    firstNamePhonetic = "";
    id = 699;
    jobTitle = "";
    lastName = "";
    lastNamePhonetic = "";
    middleName = "";
    nickName = "";
    note = "";
    phones =         (
                    {
            label = Home;
            value = "(305) 503-3957";
        }
    );
    prefix = "";
    suffix = "";
    updatedAt = "2015-09-23 23:31:25 +0000";

}

)

Thanks

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Alex Knows
  • 11
  • 2
  • 8
  • I tried to answer (deleted) but it was wrong though I don't get it -- since the other answer also can't get it right, I urge you to provide a small example we can try. – Daij-Djan Jan 04 '16 at 23:13
  • When you are extracting the info out, do a for loop: for (int i = 0; i – Mark Bourke Jan 05 '16 at 00:16
  • I solved it. I had to nest two for loops together. The first for loop will get me the "phones" key. Which had an array with another dictionary in it. The second for loop gave me the "value" key. – Alex Knows Jan 05 '16 at 01:50

2 Answers2

1

If I am understanding this correctly, on the line where you write

NSArray *value = [titles valueForKey:@"value"];,

You are trying to index the NSArray full of dictionaries using the index "value", which doesn't make sense. You should instead loop through your titles array, pull out the value from each dictionary element, and then append that element to your recipents array.

Here is some sample code that should do what I think you want.

NSArray *titles = [self.selectedUsers valueForKey:@"phones"];

NSMutableArray *recipients = [[NSMutableArray alloc] init];
for (NSDictionary* dict in titles) {
    NSString* value = [dict objectForKey:@"value"];
    [recipients addObject:value];
}
NSLog(@"Phone Numbers: %@",recipients);
72A12F4E
  • 1,744
  • 1
  • 14
  • 28
  • I'm still having issue with this code, because I'm not initializing a NSDictionary. I get back multiple dictionaries in an array from the iOS addressBook. What I want is to go though each dictionary in the array and pull out a specific value and added to a new NSMutableArray. Thanks – Alex Knows Jan 04 '16 at 22:18
  • Edited to reflect your post. – 72A12F4E Jan 04 '16 at 22:46
  • When I try this it crashes at: [dict objectForKey:@"value"]; reason: '-[__NSArrayM objectForKey:]: unrecognized selector sent to instance 0x154f258c0' – Alex Knows Jan 04 '16 at 22:51
  • phones (or titles) is an array of dictionaries – Daij-Djan Jan 04 '16 at 22:55
  • self.selectedUser is an array of selected contacts from my contact list it returns a full addressBook dictionaries that are stored in self.selectedUser. Then *titles becomes a NSArray of the @"phones" key which as two value pair inside. label = mobile and value = 786-111-1111. What I want to do is get only the phone number string and store all of them in an array to then past to the recipients. – Alex Knows Jan 04 '16 at 23:05
  • that is wrong as both this and my answer would have worked then ... phones/titles can't be an array of dictionaries with a value key each – Daij-Djan Jan 04 '16 at 23:16
0

Here is the solution I came up with.

First run a for loop to grab the first key. Then nest another for loop to grab the second key.

 NSArray *values = self.selectedUsers;
 NSMutableArray *recipients = [[NSMutableArray alloc] init];


 NSArray *values = self.selectedUsers;
 NSMutableArray *recipients = [[NSMutableArray alloc] init];


for (NSDictionary* dict in values) {

    // Grabs phones key
     NSDictionary *titles = [dict objectForKey:@"phones"];

    for (NSDictionary* dict2 in titles) {

     // Grabs the "value" key
     NSString* value = [dict2 objectForKey:@"value"];
        [recipients addObject:value];
    }

}
Alex Knows
  • 11
  • 2
  • 8