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
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);