【问题标题】:How to access the array values in objective c?如何访问目标 c 中的数组值?
【发布时间】:2017-08-03 10:54:38
【问题描述】:

电话号码的值为

(
    (
    9834677334
),
    (
    9977655456
),
    (
    9976367777
),
    (
    9654567877
),
    (
    9834777347
),
    (
    9994157837
),
    (
    9978855544
),
    (
    9873667378
)
)

代码

- (void)parseContactWithContact :(CNContact* )contact { 

 NSString * firstName = contact.givenName; 
 NSLog(@"The firstName value is %@",firstName); 
 [tableData addObject:firstName]; 
 NSString * items = [[contact.phoneNumbers valueForKey:@"value"] valueForKey:@"digits"]; 
 NSLog(@"The phone value is %@",items); 
 [phonenumber addObject:items]; 
 NSString * email = [contact.emailAddresses valueForKey:@"value"]; 
 NSLog(@"The email value is %@",email);
  NSArray * addrArr = [self parseAddressWithContac:contact]; 

 }

我已经获取了所有的联系人记录......我得到了上面提到的电话号码值,这里我的问题是,我不知道如何访问数组值......当我试图将它存储为一个数组时并填充到tableview中,我得到一个错误,我只能访问第一个值..谁能帮助我如何访问和填充到tableview中

【问题讨论】:

  • 我只能访问第一个值 --> 你得到第 0 个索引,显示你尝试过的代码
  • 分享一些你试过的代码。
  • - (void)parseContactWithContact :(CNContact* )contact { NSString * firstName = contact.givenName; NSLog(@"名字的值为 %@",firstName); [tableData addObject:firstName]; NSString * items = [[contact.phoneNumbers valueForKey:@"value"] valueForKey:@"digits"]; NSLog(@"电话值为%@",items); [电话号码添加对象:项目]; NSString * email = [contact.emailAddresses valueForKey:@"value"]; NSLog(@"邮箱值为%@",email); NSArray * addrArr = [self parseAddressWithContac:contact]; }
  • @M.Kavitha - 你在哪里使用了这个phonenumber,显示一些额外的代码
  • 同时打印这个NSLog(@"The phone value is %@",items);

标签: ios objective-c arrays json uitableview


【解决方案1】:

试试这个

NSString * items = [[contact.phoneNumbers valueForKey:@"value"] valueForKey:@"digits"]; 
NSLog(@"The phone value is %@",items); 
[phonenumber addObject:items]; 

删除电话号码中的特殊字符 () 并将您的字符串附加到 phonenumber 数组并填充到您的表格中

例如

NSString * items = [[contact.phoneNumbers valueForKey:@"value"] valueForKey:@"digits"]; 
NSCharacterSet *filterstr = [[NSCharacterSet characterSetWithCharactersInString:@"1234567890"] invertedSet];
NSString *resultString = [[items componentsSeparatedByCharactersInSet:filterstr] componentsJoinedByString:@""];
[phonenumber addObject:items]; 
.... continue your same work ...

你得到电话号码数组

【讨论】:

  • 此代码不起作用 ....*** 由于未捕获的异常 'NSInvalidArgumentException' 导致应用程序终止,原因:'-[__NSArrayI componentsSeparatedByCharactersInSet:]:无法识别的选择器发送到实例 0x7fbe80e03000'
  • @M.Kavitha - 哈哈哈,我知道了,你能打印这个NSLog(@"The phone value is %@",items)
  • 项目值为(9834677334)
  • @M.Kavitha - 如果你得到的结果是( 9834677334 ),我的代码肯定能工作超过 100%
  • 对不起,我不工作...NSString *resultString = [[items componentsSeparatedByCharactersInSet:filterstr] componentsJoinedByString:@""];....这里我遇到了 UIKit 错误
猜你喜欢
  • 2020-07-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-15
  • 2021-08-12
相关资源
最近更新 更多