收集字符串值并添加到数组中

时间:2013-06-05 05:09:21

标签: ios objective-c

我正在编写一个应用程序,我想从列表中收集姓名和电话号码并将其添加到数组中。我该怎么做呢?我可以检索名字和姓氏,但我不知道如何在下面的代码中添加电话号码,因为它位于不同的 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]);
    }
}

所以在上面的代码中,我能够从列表中获取名字和姓氏,并将它们添加到数组中,类似地,我如何获取手机号码并添加到同一个数组中并获得{{ {1}}函数,因为它是另一个displayNames:循环。有人可以编辑代码并告诉我在上面的代码中我必须做出哪些更改。同样在结果中,为什么会显示两次呢?

4 个答案:

答案 0 :(得分: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);

答案 1 :(得分: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];

答案 2 :(得分:1)

因此,使用Array,您必须使用DictionaryFirstName, LastName and MobileNo对存储keyvalue。如果您有多个用户而不是使用Array作为上层,则意味着将您的用户词典添加到数组中,以及何时您希望用户编写代码:

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

这是其中一种方式......

答案 3 :(得分: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");


    }

}

在我的申请中。我正在使用此代码将Addressbook内容存储到Array。我希望对你有帮助。