【问题标题】:iOS:saving string data in arrayiOS:将字符串数据保存在数组中
【发布时间】:2014-10-04 04:41:16
【问题描述】:

我正在开发联系人列表应用程序,因此我需要带上联系人数据并存储在我做的表格视图中,如下所示

     - (void)listPeopleInAddressBook:(ABAddressBookRef)addressBook
   {
     NSInteger numberOfPeople = ABAddressBookGetPersonCount(addressBook);
    contactList= CFBridgingRelease(ABAddressBookCopyArrayOfAllPeople(addressBook));

for (int i = 0; i < numberOfPeople; i++)
{
    ABRecordRef person = (__bridge ABRecordRef)contactList[i];

   firstName = CFBridgingRelease(ABRecordCopyValue(person, kABPersonFirstNameProperty));
 lastName  = CFBridgingRelease(ABRecordCopyValue(person, kABPersonLastNameProperty));
    NSLog(@"Name:%@ %@", firstName, lastName);

    ABMultiValueRef phoneNumbers = ABRecordCopyValue(person, kABPersonPhoneProperty);

    CFIndex numberOfPhoneNumbers = ABMultiValueGetCount(phoneNumbers);
    for (CFIndex i = 0; i < numberOfPhoneNumbers; i++)
    {
        NSString *phoneNumber = CFBridgingRelease(ABMultiValueCopyValueAtIndex(phoneNumbers, i));
        NSLog(@"  phone is:%@", phoneNumber);
    }
         CFRelease(phoneNumbers);

           }


        }

旨在以字符串格式获取数据,即名字和姓氏。我的问题是我需要将名字和姓氏中存在的数据保存到数组但该数组存在于 for 之外环形 。这样我就可以将数组数据传递给表格视图。

【问题讨论】:

    标签: ios objective-c abaddressbook


    【解决方案1】:

    1.option 1:只需做一件事,在 for 循环中创建 NSMutableDictionary。将所有(名字,姓氏等)存储在其中。最后 for 循环将其添加到数组中。 2.option 2:将联系人创建为具有所需属性的对象。请参阅此处的示例https://stackoverflow.com/questions/24552545/how-to-get-phonebook-contacts-from-ios-device

    【讨论】:

      【解决方案2】:
      - (void)listPeopleInAddressBook:(ABAddressBookRef)addressBook
      {
      
          NSInteger numberOfPeople = ABAddressBookGetPersonCount(addressBook);
          NSMutableArray * contactList= CFBridgingRelease(ABAddressBookCopyArrayOfAllPeople(addressBook));
      
          NSMutableArray *tableviewArray =[[NSMutableArray alloc] init]; // ADD This Line
      
          for (int i = 0; i < numberOfPeople; i++)
          {
              ABRecordRef person = (__bridge ABRecordRef)contactList[i];
      
              NSString *  firstName = CFBridgingRelease(ABRecordCopyValue(person, kABPersonFirstNameProperty));
              NSString *  lastName  = CFBridgingRelease(ABRecordCopyValue(person, kABPersonLastNameProperty));
              NSLog(@"Name:%@ %@", firstName, lastName);
      
      
      
      
              ABMultiValueRef phoneNumbers = ABRecordCopyValue(person, kABPersonPhoneProperty);
      
              CFIndex numberOfPhoneNumbers = ABMultiValueGetCount(phoneNumbers);
              for (CFIndex i = 0; i < numberOfPhoneNumbers; i++)
              {
                  NSString *phoneNumber = CFBridgingRelease(ABMultiValueCopyValueAtIndex(phoneNumbers, i));
                  NSLog(@"  phone is:%@", phoneNumber);
              }
      
              [tableviewArray addObject:[NSDictionary dictionaryWithObjectsAndKeys:firstName , @"first_name" , lastName , @"last_name" , nil]]; // ADD This Line
      
              CFRelease(phoneNumbers);
      
          }
      
      
      }
      

      【讨论】:

      • 因为你已经给出了 tableview 数组只适用于那个循环如果数组从那个循环中出来它返回空值我需要保持数据相同,当它被保存在不同的方法中时,电话号码呢提前谢谢请帮忙。
      • 创建一个全局数组
      • kk 我知道了,但我的目标是只为最后一个索引名称获取电话号码,这意味着它只显示 las 姓名的电话号码不是所有请告诉我
      • NSString *phoneNumber = CFBridgingRelease(ABMultiValueCopyValueAtIndex(phoneNumbers, numberOfPhoneNumbers - 1)); [tableviewArray addObject:[NSDictionary dictionaryWithObjectsAndKeys:firstName , @"first_name" , lastName , @"last_name" , phoneNumber , @"phone_no", nil]]; // 添加这一行
      • kk 谢谢你成功了,我们能不能用搜索栏过滤名字&no 你能给我任何例子,我试过很多,但我找不到 id跨度>
      猜你喜欢
      • 2023-03-14
      • 2023-02-07
      • 1970-01-01
      • 1970-01-01
      • 2011-10-18
      • 2020-01-02
      • 2021-06-19
      • 2013-11-30
      • 1970-01-01
      相关资源
      最近更新 更多