【问题标题】:Error getting information from address book从通讯录获取信息时出错
【发布时间】:2012-09-27 04:06:55
【问题描述】:

我在从通讯簿中检索特定数据数组时遇到问题。代码如下:

- (void) getContacts{    
    ABAddressBookRef ab = ABAddressBookCreate();
    NSMutableArray *retVal = (__bridge NSMutableArray *)(ABAddressBookCopyArrayOfAllPeople(ab));
    CFRelease(ab);

    contact* temp=[[contact alloc] init];
    NSMutableArray* tempArray=[[NSMutableArray alloc]init];

    for(int i=0;i<[retVal count];i++)
        [tempArray insertObject:
          [NSString stringWithFormat:@"%@",
            [temp set2:[NSString stringWithFormat:@"%@",
              [[retVal objectAtIndex:i] kABPersonFirstNameProperty]] 
                  last:[NSString stringWithFormat:@"%@",
                    [[retVal objectAtIndex:i] kABPersonLastNameProperty]] 
                number:[NSString stringWithFormat:@"%@",
                  [[retVal objectAtIndex:i] kABPersonPhoneProperty]]]] atIndex:i];

    _objects=tempArray;

    [self alert:[NSString stringWithFormat:@"%@",_objects] title:@"TEMP"];
}

我得到的错误是关于 kABPerson 属性的。

更多说明:本质上,我将地址簿中的所有数据抓取到第一个数组中,然后我手动遍历该数组并尝试检索我的应用程序其余部分所需的数据。

有什么想法吗?

Just for more clarification here's my contact.h file:

@interface contact : NSString{
    NSString* first;
    NSString* last;
    NSString* number;
}
@end

这是我的contact.m文件:

@implementation contact

- (void) set:(NSString*)first2 last:(NSString*)last2 number:(NSString*)number2{
    first=first2;
    last=last2;
    number=number2;
}

- (contact*) set2:(NSString*)first2 last:(NSString*)last2 number:(NSString*)number2{
    first=first2;
    last=last2;
    number=number2;

    return self;
}

@end

这行似乎太长,无法发布:

//Enter contact into tempArray
    [tempArray insertObject:[NSString stringWithFormat:@"%@",[temp set:[NSString stringWithFormat:@"%@",(__bridge NSString *)(ABRecordCopyValue((__bridge ABMultiValueRef)[retVal objectAtIndex:i],kABPersonFirstNameProperty))] last:[NSString stringWithFormat:@"%@",(__bridge NSString *)(ABRecordCopyValue((__bridge ABMultiValueRef)[retVal objectAtIndex:i],kABPersonLastNameProperty))] number:(__bridge NSString *)ABMultiValueCopyValueAtIndex((__bridge ABMultiValueRef)((__bridge NSString*)ABRecordCopyValue((__bridge ABRecordRef)([retVal objectAtIndex:i]),kABPersonPhoneProperty)), 0)]] atIndex:i];

【问题讨论】:

  • 你应该说出实际的错误是什么。
  • 确实如此,抱歉。 1) 'contact' 没有可见的@interface 声明选择器...意味着它不知道该方法是什么 2) 选择器 kAB 没有已知的实例方法...
  • 顺便声明一下……
  • 我修复了第一个错误:[tempArray insertObject:[NSString stringWithFormat:@"%@",[contact set2:[NSString stringWithFormat:@"%@",[[retVal objectAtIndex:i] kABPersonFirstNameProperty ]] last2:[NSString stringWithFormat:@"%@",[[retVal objectAtIndex:i] kABPersonLastNameProperty]] number2:[NSString stringWithFormat:@"%@",[[retVal objectAtIndex:i] kABPersonPhoneProperty]]]] atIndex:我];

标签: ios objective-c iphone cocoa


【解决方案1】:

kABPersonFirstNameProperty 是属性 ID,而不是 Objective-C 对象属性。

也就是说,你不能使用[[retVal objectAtIndex:i] kABPersonFirstNameProperty]——你需要像这样访问名字和姓氏:

CFStringRef firstName = ABRecordCopyValue ([retVal objectAtIndex:i], kABPersonFirstNameProperty);
CFStringRef lastName = ABRecordCopyValue ([retVal objectAtIndex:i], kABPersonLastNameProperty);
CFStringRef phoneNum = ABRecordCopyValue ([retVal objectAtIndex:i], kABPersonPhoneProperty);

然后不要忘记 ABRecordCopyValue 遵循创建规则 - 之后您需要 CFRelease(firstName)

【讨论】:

  • @brian 是的,在玩了几个小时之后,我确实注意到了这一点。我已经好几天没有检查板子了。我已经处理了以前的错误,但现在由于某种原因在这一行中出现了内存错误:
  • 该行应该做的是单独挑选手机号码而不返回多值
猜你喜欢
  • 2010-12-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-11
  • 1970-01-01
  • 1970-01-01
  • 2012-10-27
  • 1970-01-01
相关资源
最近更新 更多