1

我正在编写一个应用程序,我想从列表中收集姓名和电话号码并将它们添加到数组中。我该怎么做呢?我可以检索名字和姓氏,但我不知道如何在下面的代码中添加电话号码,因为它在不同的for循环中。它可能看起来很简单,但我被困住了,因为我是新手。

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

所以在上面的代码中,我能够从列表中获取名字和姓氏并将它们添加到数组中,同样我如何获取手机号码并添加到同一个数组中并在displayNames:函数中得到结果另一个for循环。有人可以编辑代码并告诉我必须在上面的代码中进行哪些更改。同样在结果中,所有内容都显示了两次,为什么?

4

4 回答 4

2
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);
于 2013-06-05T07:28:29.903 回答
1

您可以尝试在字典中添加用户信息或创建模型用户对象并存储在数组中。因此,所有信息都封装在一个对象中。

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

    }

您可以枚举使用

[self.users enumerateObjectsUsingBlock:^(NSDictionary * userInfo, NSUInteger idx, BOOL *stop) {

        NSString *firstName = userInfo[@"FirstName"];
        NSString *mobileNumber = userInfo[@"MobileNumber"];

    }];

搜索单个用户的名称

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"FirstName == %@",@"aUserName"];
NSDictionary *userInfo = [[self.users filteredArrayUsingPredicate:predicate]lastObject];
于 2013-06-05T05:11:48.670 回答
1

因此,Array您必须使用Pair来Dictionary存储而不是使用。如果您有多个用户而不是用作上层,则意味着将您的用户字典添加到数组中,并且当您希望用户编写代码时:FirstName, LastName and MobileNokeyvalueArray

for(NSDictionary *userDic in yourArray)
{
    NSString *fName = [userDic valueForKey:@"FirstName"];
    ...
}

这是其中一种方式...

于 2013-06-05T05:28:24.257 回答
0

尝试以下步骤:

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


    }

}

在我的应用程序中。我正在使用此代码将地址簿内容存储到数组。我希望它对你有帮助。

于 2013-06-05T05:28:26.963 回答