Reputation: 35
I am writing an app where I want to collect names and phone numbers from a list and add them into an array. How do I do this? I could retrieve first name and last name, but I don't get how to add a phone number in the below code as it is in a different for
loop. It might look simple but I am stuck as I am new.
for (i = 0; i < [list count]; i++)
{
NSString *firstName = (__bridge_transfer NSString *)ABRecordCopyValue(contactPerson, kABPersonFirstNameProperty);
NSString *lastName = (__bridge_transfer NSString *)ABRecordCopyValue(contactPerson, kABPersonLastNameProperty);
NSMutableArray *name = [NSMutableArray array];
if(firstName != nil)
[name addObject:firstName];
if(lastName != nil)
[name addObject:lastName];*/
[self displaynames:name];
ABMultiValueRef mobile=ABRecordCopyValue(contactPerson, kABPersonPhoneProperty);
for (int k=0;k<ABMultiValueGetCount(mobile); k++)
{
NSString *mobileNo = (__bridge_transfer NSString *)ABMultiValueCopyValueAtIndex(mobile, k);
NSLog(@"mobile number: %@",mobileNo);
}
}
- (void)displaynames:(NSMutableArray*)names
{
for (NSMutableArray* name in names)
{
NSLog(@"MyResult:%@ %@",[names objectAtIndex:0],[names objectAtIndex:1]);
}
}
So in the above code I am able to get the first name and last name from the list and add them into the array, similarly how do I get mobile phone number and add into the same array and get the result in the displayNames:
function as it is another for
loop. Can someone please edit the code and tell me what changes I have to make in the above code. Also in the result everything is being displayed twice why so?
Upvotes: 1
Views: 1270
Reputation: 12641
NSMutableArray *contactList=[[NSMutableArray alloc] init];
for (int i=0; i<4; i++) {
NSMutableDictionary *contactInfo=[[NSMutableDictionary alloc] init];
for (int i = 0; i < [list count]; i++)
{
NSString *firstName = (__bridge_transfer NSString *)ABRecordCopyValue(contactPerson, kABPersonFirstNameProperty);
NSString *lastName = (__bridge_transfer NSString *)ABRecordCopyValue(contactPerson, kABPersonLastNameProperty);
if (![firstName isEqualToString:@""]) {
[contactInfo setValue:firstName forKey:@"firstName"];
}
if (![lastName isEqualToString:@""]) {
[contactInfo setValue:lastName forKey:@"lastName"];
}
NSMutableArray *mobileNoArray=[[NSMutableArray alloc] initWithCapacity:ABMultiValueGetCount(mobile)];
ABMultiValueRef mobile=ABRecordCopyValue(contactPerson, kABPersonPhoneProperty);
for (int k=0;k<ABMultiValueGetCount(mobile); k++)
{
NSString *mobileNo = (__bridge_transfer NSString *)ABMultiValueCopyValueAtIndex(mobile, k);
[mobileNoArray addObject:mobileNo];
}
if (mobileNoArray.count!=0) {
[contactInfo setObject:mobileNoArray forKey:@"mobileNo"]
}
}
[contactList addObject:contactInfo];
NSLog(@"contact info == %@",contactInfo);
}
NSLog(@"contact list array is %@",contactList);
Upvotes: 2
Reputation: 10201
You can try to add the user info in a dictionary or create a model user object and store in the array. So all the information stays encapsulated in a single object.
self.users = [NSMutableArray array];
for (i = 0; i < [list count]; i++)
{
NSString *firstName = ...
NSString *lastName = ...
NSString *mobileNumber = ...
NSMutableDictionary *userInfo = [NSMutableDictionary dictionary];
if(firstName)
userInfo[@"FirstName"] = firstName;
if(lastName)
userInfo[@"LastName"] = lastName;
if(mobileNumber)
userInfo[@"MobileNumber"] = mobileNumber;
[self.users addObject:userInfo];
}
You can enumerate using
[self.users enumerateObjectsUsingBlock:^(NSDictionary * userInfo, NSUInteger idx, BOOL *stop) {
NSString *firstName = userInfo[@"FirstName"];
NSString *mobileNumber = userInfo[@"MobileNumber"];
}];
Searching for a single user for a name
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"FirstName == %@",@"aUserName"];
NSDictionary *userInfo = [[self.users filteredArrayUsingPredicate:predicate]lastObject];
Upvotes: 1
Reputation:
Try the following steps:
NSMutableArray *firstNames;
NSMutableArray *LastNames;
NSMutableArray *Mobile_numbers;
NSMutableArray *type_array;
NSMutableArray *firstandlast;
........
-(void)get_arr
{
ABAddressBookRef addressBook = ABAddressBookCreate();
NSArray *arrayOfPeople = (__bridge_transfer NSArray *)ABAddressBookCopyArrayOfAllPeople(addressBook);
NSUInteger index = 0;
firstNames = [[NSMutableArray alloc] init];
Mobile_numbers = [[NSMutableArray alloc] init];
type_array=[[NSMutableArray alloc]init ];
LastNames=[[NSMutableArray alloc]init ];
NSMutableArray *firstandlast;
@try
{
for(index = 0; index<=([arrayOfPeople count]-1); index++)
{
ABRecordRef currentPerson = (__bridge ABRecordRef)[arrayOfPeople objectAtIndex:index];
NSString *type;
ABMultiValueRef phones = (ABMultiValueRef)ABRecordCopyValue(currentPerson, kABPersonPhoneProperty);
for (int i=0; i < ABMultiValueGetCount(phones); i++)
{
//NSString *phone = (NSString *)ABMultiValueCopyValueAtIndex(phones, i);
////NSLog(@"%@", phone);
mobileLabel = (NSString*)ABMultiValueCopyLabelAtIndex(phones, i);
//NSLog(@"MOG:%@",mobileLabel);
if([mobileLabel isEqualToString:@"_$!<Mobile>!$_"]||[mobileLabel isEqualToString:@"_$!<Main>!$_"])
{
//NSLog(@"mobile:");
type=@"mobile";
}
else if ([mobileLabel isEqualToString:@"_$!<Work>!$_"])
{
//NSLog(@"Work:");
type=@"Work";
}
else if ([mobileLabel isEqualToString:@"_$!<Home>!$_"])
{
//NSLog(@"Home:");
type=@"Home";
}
else if ([mobileLabel isEqualToString:@"_$!<Other>!$_"] )
{
//NSLog(@"Other:");
type=@"Other";
}
mobile = (NSString*)ABMultiValueCopyValueAtIndex(phones, i);
//NSLog(@"GG:%@",mobile);
mobile = [mobile stringByReplacingOccurrencesOfString:@"-"
withString:@""];
mobile = [mobile stringByReplacingOccurrencesOfString:@"("
withString:@""];
mobile = [mobile stringByReplacingOccurrencesOfString:@")"
withString:@""];
mobile = [mobile stringByReplacingOccurrencesOfString:@" "
withString:@""];
[Mobile_numbers addObject:mobile];
[type_array addObject:type];
NSString *currentFirstName = (__bridge_transfer NSString *)ABRecordCopyValue(currentPerson, kABPersonFirstNameProperty);
////NSLog(@"NAME:%@",currentFirstName);
if ([currentFirstName length]!=0)
{
//NSLog(@"NAME:%@",currentFirstName);
[firstNames addObject:currentFirstName];
}
else
{
//NSLog(@"NAME:DUMMY");
currentFirstName=@"";
[firstNames addObject:currentFirstName];
}
NSString *currentLast = (__bridge_transfer NSString *)ABRecordCopyValue(currentPerson, kABPersonLastNameProperty);
////NSLog(@"NAME:%@",currentFirstName);
if ([currentLast length]!=0)
{
//NSLog(@"NAME:%@",currentLast);
[LastNames addObject:currentLast];
}
else
{
//NSLog(@"NAME:DUMMY");
currentLast=@"";
[LastNames addObject:currentLast];
}
NSString *temp_f_l=[NSString stringWithFormat:@"%@ %@",currentFirstName,currentLast];
[firstandlast addObject:temp_f_l];
}
NSLog(@"MOB:%@",Mobile_numbers);
NSLog(@"TYPE:%@",type_array);
NSLog(@"FN:%@",firstNames);
NSLog(@"FN:%@",LastNames);
NSLog(@"FN&LN:%@",firstandlast);
}
}
@catch (NSException *exception)
{
//NSLog(@"CATCH");
}
}
In my Application. I am using this code to store Addressbook contents to Array. I hope it help for you.
Upvotes: 0
Reputation: 1544
So, instead using Array
you have to use Dictionary
to store FirstName, LastName and MobileNo
with keyvalue
Pair. If you have multiple user than use Array
as upper layer, means add your user dictionary into array and when ever you want a user write the code:
for(NSDictionary *userDic in yourArray)
{
NSString *fName = [userDic valueForKey:@"FirstName"];
...
}
This is one of the way...
Upvotes: 1