kABPersonFirstNameProperty... 拖曳 EXC_BAD_ACCESS

发布于 2024-08-21 19:37:15 字数 1177 浏览 9 评论 0原文

我正在阅读地址簿联系人...一切都很顺利,直到我测试了一个没有的联系人 名字(因为我可以仅使用电子邮件或电话或其他方式创建联系人......)。 代码(简化)是这样的:

- (NSMutableArray *) getContactsInfo {
    NSMutableArray *contactsList = [[NSMutableArray alloc] init];
    localAddressBook = ABAddressBookCreate();

    int contactsLength = (int)ABAddressBookGetPersonCount(localAddressBook);

    if (contactsLength < 1)
        return nil;

    for(int currentContact=1; currentContact < (contactsLength + 1); currentContact++) {
        ABRecordRef person = ABAddressBookGetPersonWithRecordID(localAddressBook,(ABRecordID) currentContact);

        firstName = (NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty);
        NSLog(@"%@", firstName);

        [contactsList addObject:firstName];
        CFRelease(person);
    }

    return contactsList;
}

我得到的输出是这样的:

2010-02-15 14:16:25.616 testApp[7065:207] Contact0
2010-02-15 14:16:25.618 testApp[7065:207] Contact1
2010-02-15 14:16:25.619 testApp[7065:207] Contact2
Program received signal:  “EXC_BAD_ACCESS”.

我有 3 个联系人,名字和姓氏 出于测试目的,仅使用姓氏创建了一个。

看来我可以正确读取任何属性,例如电子邮件或带有数组的地址...但是当联系人缺少名字属性时,应用程序崩溃。

Im reading the address book contacts... everything goes well until I test a contact with no
First Name ( Since I can create a contact with just an email or a phone or wathever....).
The code (reduced) is this:

- (NSMutableArray *) getContactsInfo {
    NSMutableArray *contactsList = [[NSMutableArray alloc] init];
    localAddressBook = ABAddressBookCreate();

    int contactsLength = (int)ABAddressBookGetPersonCount(localAddressBook);

    if (contactsLength < 1)
        return nil;

    for(int currentContact=1; currentContact < (contactsLength + 1); currentContact++) {
        ABRecordRef person = ABAddressBookGetPersonWithRecordID(localAddressBook,(ABRecordID) currentContact);

        firstName = (NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty);
        NSLog(@"%@", firstName);

        [contactsList addObject:firstName];
        CFRelease(person);
    }

    return contactsList;
}

and the output I get is this:

2010-02-15 14:16:25.616 testApp[7065:207] Contact0
2010-02-15 14:16:25.618 testApp[7065:207] Contact1
2010-02-15 14:16:25.619 testApp[7065:207] Contact2
Program received signal:  “EXC_BAD_ACCESS”.

I have 3 contacts with First and Last names
And one created with just the last name, for test purposes.

It seems I can properly read any property such as email or address with arrays... but when a contact lacks First Name Property the app crashes.

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

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

发布评论

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

评论(4

悲欢浪云 2024-08-28 19:37:15

您在代码中做了一些非常错误的事情:您假设记录 ID 是连续的并且从 1 开始。事实并非如此,您不能依赖这一点。

您应该做的是使用 ABAddressBookCopyArrayOfAllPeople 查找地址簿中的所有记录,然后使用 Core Foundation CFArray 函数获取各个项目。

(是的,iPhone中的通讯录API很糟糕)

You are doing something very wrong in your code: you are assuming that the record IDs are sequential and starting at 1. This is not the case at all, you cannot rely on this.

What you should do instead is use ABAddressBookCopyArrayOfAllPeople to find all records in the Address Book and then use the Core Foundation CFArray functions to get to the individual items.

(Yes, the Address Book API in the iPhone is terrible)

日裸衫吸 2024-08-28 19:37:15

您可能需要启用 NSZombies准确查看 EXEC_BAD_ACCESS 的来源。

You may want to enable NSZombies to see exactly where the EXEC_BAD_ACCESS is coming from.

怕倦 2024-08-28 19:37:15

为了确保崩溃发生在 ABRecordCopyValue 内,而不是在您第一次尝试使用 firstName 时发生(可能为 NULL? )另外,person也不为NULL,对吗? (一般来说,问题中的更多代码以及有关哪一行崩溃的详细信息会很有帮助。)

另一件事可以尝试是将 person 转换为 ABRecord* 并使用[valueForProperty][1];这两种类型是免费桥接的,您可能会从后者中得到不同的结果(尽管我对此表示怀疑)。

更新:鉴于您发布的代码,您需要在尝试通过 NSLog 输出它之前检查 firstName 不为 NULL - 这很有可能ABRecordCopyValue 只是返回 NULL(表示该记录不存在名字数据。)您还应该检查 person 引用值本身的有效性 - 传递ABRecordCopyValue 中的 NULL 可能是其他问题的根源。

To make sure, the crash is taking place within ABRecordCopyValue and not when you try to use firstName for the first time (which may be NULL?) Also, person is not NULL either, correct? (In general more code in the question along with details about which line is crashing would be helpful.)

Another thing to try would be to cast person to an ABRecord* and use [valueForProperty][1]; the two types are toll-free bridged and you might get a different result out of the latter (though I doubt it).

Update: Given the code you have posted you need to check that firstName is not NULL before trying to output it via NSLog - it is very possible ABRecordCopyValue is simply returning NULL (representing the fact there is no first name data present for that record.) You should also check for the validity of the person ref value itself - passing NULL in person to ABRecordCopyValue could be the source of additional problems.

半世晨晓 2024-08-28 19:37:15

问题确实是一个 nil 名字 - 但不是在日志语句中,而是在你尝试将 nil 插入数组的地方。您不能将 nil 值插入数组,这会导致崩溃。 NSLog 尚未将输出刷新到控制台,这就是为什么您还没有看到最后一个日志语句说名字是 nil 的原因。

每当您从地址簿中获取数据时,请在将其插入任何内容之前检查该值是否为零。

The problem is indeed a nil first name - but not at the log statement, rather where you try to insert nil into the array. You cannot insert a nil value into an array, that causes a crash. NSLog has not flushed output to the console yet which is why you do not yet see your last log statement saying first name is nil.

Any time you get data out of the address book, check to see if the value is nil before you insert it into anything.

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