0

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

- (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];
4

1 回答 1

0

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)之后您需要这样做。

于 2012-09-27T15:28:17.447 回答