0

我正在尝试从 iPhone 的电话簿中获取所有电话号码。但是,一旦我尝试使用 kABPersonPhoneProperty 获取号码,我的应用程序就会崩溃。代码片段在这里

ABAddressBookRef addressBook = ABAddressBookCreate(); 
NSMutableArray *allPeople = (NSMutableArray*)ABAddressBookCopyArrayOfAllPeople(addressBook); 
int nPeople = ABAddressBookGetPersonCount(addressBook); 
CFRelease(addressBook);

for(int i=0; i < nPeople; i++ ){

    ABRecordRef person = [allPeople objectAtIndex:i];
    NSString *name = @"";
    CFTypeRef fName = ABRecordCopyValue(person, kABPersonFirstNameProperty);
    CFTypeRef lName = ABRecordCopyValue(person, kABPersonLastNameProperty);
    ABMultiValueRef multi = ABRecordCopyValue(person , kABPersonPhoneProperty);
    NSString *number = (NSString *)ABMultiValueCopyValueAtIndex(multi, 0);

    if ([number length] > 0) {
        if(fName != NULL)
        {
            if (lName != NULL) {
                name = [[NSString stringWithFormat:@"%@", fName] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
            }
            else {
                name = [[NSString stringWithFormat:@"%@", fName] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
            }
            NSLog(@"Number==>%@ ",number);
        }
    }
    if (fName != NULL) {
        CFRelease(fName);
    }
    if (lName != NULL) {
        CFRelease(lName);
    }
    if (multi != NULL) {
        CFRelease(multi);
    }
}

[allPeople release];

我无法弄清楚其中的错误。即使运行不顺利,我也会发布所有内容。

在运行构建和分析代码部分时,我什至会遇到潜在的内存泄漏

ABMultiValueRef multi = ABRecordCopyValue(person , kABPersonPhoneProperty);
    NSLog(@"Number===>%d" , ABMultiValueGetCount(multi));

请帮我出来。

任何形式的帮助将不胜感激。

提前致谢

4

1 回答 1

0

感谢您的回复。我已经找到了解决问题的方法。我没有释放数字变量,认为引用是作为引用传递的,但它被复制到返回的位置,因此需要在使用后释放数字。

再次感谢所有回复!

于 2011-03-04T07:35:03.570 回答