使用 kABPersinPhoneProperty 时应用程序崩溃

发布于 2024-10-20 01:58:43 字数 1744 浏览 4 评论 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));

请帮助我。

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

提前致谢

I am trying to get all the phone number from the Phone Book of iPhone. But my app crashes as soon as I try to fetch the number using kABPersonPhoneProperty. The code snippet is here

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

I am unable to figure out the error in this. I am also releasing everything even then it is not running smoothly.

I am even getting potential memory leak when running build and analyze for the code part

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

Please help me out to come.

Any kind of help would be highly appreciated.

Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

探春 2024-10-27 01:58:43

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

再次感谢所有的回复!

Thanks for the response. I have figured out the solution fot the problem here. I did not release number variable thinking that the reference was passed as reference but it is copied to the returned location, hence need to release number after the usage.

thanks again for all the responses!!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文