2013-06-05 61 views
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循環。有人可以編輯代碼並告訴我在上面的代碼中必須做出什麼改變。爲什麼所有結果都顯示兩次?

回答

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

非常感謝...它的工作原理,但它只顯示一個列表,如果我想要顯示完整列表我在上面的代碼中做了哪些更改? – user2454248

+0

因爲您有kABPersonFirstNameProperty,只需將其他人添加到字典中即可。 – Warewolf

+0

當我檢查contactInfo只有一個名字lastname和一個人的電話號碼保持其不displayin..if我有4個聯繫人我想要顯示所有這些..所以最新的變化你做你的上述代碼。 – user2454248

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

因此上述方法是不正確的?我想這種方式,因爲我想要的結果數據發送給其他一些functionality..So你可以請編輯代碼和我是新來的ios .. – user2454248

+0

所以如果我想在這個函數之外使用userinfo來獲得更多功能我如何使用這個?bcoz我需要在對等會話中發送所有這些數據這就是爲什麼。在上面的代碼中,移動號碼在循環中是不同的。同樣,用戶在上面的代碼中。 – user2454248

+0

您可以看到所有信息都添加到屬性數組中。如果你想以串行方式顯示,你可以枚舉。如果你想訪問特定的信息,你可以使用'NSPredicate'進行過濾。 – Anupdas

1

所以,而是採用Array你必須使用Dictionary存儲FirstName, LastName and MobileNokeyvalue對。如果您有使用Array作爲上層多個用戶,意味着新增用戶字典進入陣列和當過你想要一個用戶寫代碼:

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

這其中的方法之一...

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:"); 
        [email protected]"mobile"; 
       } 
       else if ([mobileLabel isEqualToString:@"_$!<Work>!$_"]) 
       { 
        //NSLog(@"Work:"); 
        [email protected]"Work"; 
       } 
       else if ([mobileLabel isEqualToString:@"_$!<Home>!$_"]) 
       { 
        //NSLog(@"Home:"); 
        [email protected]"Home"; 
       } 

       else if ([mobileLabel isEqualToString:@"_$!<Other>!$_"]) 
       { 
        //NSLog(@"Other:"); 
        [email protected]"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"); 
        [email protected]""; 
        [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"); 
        [email protected]""; 
        [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"); 


    } 

} 

在我應用。我正在使用此代碼將地址簿內容存儲到數組。我希望它對你有幫助。

+0

我想從列表中收集所有名字,姓氏和手機號碼,並使用同級會話發送,所以我如何收集所有名字和姓氏手機號碼 – user2454248

+0

我的代碼說明名字,姓氏和手機號碼數組。試試這個代碼在你的應用程序中,並檢查NSLog它必須幫助。 – 2013-06-05 05:40:36

+0

它的工作?讓我,我會幫助 – 2013-06-05 05:47:16

相關問題