IPhone AddressBook 方法返回或创建 ABRecord

发布于 2025-01-06 22:04:40 字数 949 浏览 0 评论 0原文

我的地址簿和内存确实有问题。我希望它返回一条记录(现有记录或新记录),我可以在其中保存联系人。根据构建和分析工具,这个函数有很多错误。我已经阅读了文档并重写了很多次,但我似乎无法正确理解。

编写这个方法的正确方法是什么? / 我做错了什么?

//Searches Groups by name for matches and returns the attached record
- (ABRecordRef)getGroup
{   
    ABRecordRef response;

NSArray *groups = (NSArray*)ABAddressBookCopyArrayOfAllGroups(addressBook);

for (id group in groups)
{
    NSString *currentGroup = (NSString*)ABRecordCopyValue(group, kABGroupNameProperty);
    if ([currentGroup isEqualToString:GroupName])
    {
        response = group;
        CFRelease(currentGroup); //??
        break;
    }
    CFRelease(currentGroup); //?
}

[groups release];

if (response == NULL)
{
    response = ABGroupCreate();
    ABRecordSetValue(response, kABGroupNameProperty, GroupName, &error);
    ABAddressBookAddRecord(addressBook, response, &error);
    ABAddressBookSave(addressBook, &error);
}
return response;

}

I'm having real trouble with the AddressBook and memory. I want it to return a record (either the existing one or a new one) that I can save a contact into. There is a lot wrong with this function according to the build and analyze tool. I have read the documentation and re-written this a bunch of times but I cant seem to get it right.

Whats the correct way to write this method? / What am I doing wrong?

//Searches Groups by name for matches and returns the attached record
- (ABRecordRef)getGroup
{   
    ABRecordRef response;

NSArray *groups = (NSArray*)ABAddressBookCopyArrayOfAllGroups(addressBook);

for (id group in groups)
{
    NSString *currentGroup = (NSString*)ABRecordCopyValue(group, kABGroupNameProperty);
    if ([currentGroup isEqualToString:GroupName])
    {
        response = group;
        CFRelease(currentGroup); //??
        break;
    }
    CFRelease(currentGroup); //?
}

[groups release];

if (response == NULL)
{
    response = ABGroupCreate();
    ABRecordSetValue(response, kABGroupNameProperty, GroupName, &error);
    ABAddressBookAddRecord(addressBook, response, &error);
    ABAddressBookSave(addressBook, &error);
}
return response;

}

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

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

发布评论

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

评论(1

御守 2025-01-13 22:04:41

只获取记录 ID 更容易、更可靠。
如果在不相关的帖子中发现此内容 - https://stackoverflow.com/a/8249975/874927

- (BOOL)checkIfGroupExists
{   
    BOOL hasGroup = NO;

    CFIndex groupCount = ABAddressBookGetGroupCount(addressBook);
    CFArrayRef groups = ABAddressBookCopyArrayOfAllGroups(addressBook);

    for (int i = 0; i < groupCount; i++)
    {
        ABRecordRef currentGroup = CFArrayGetValueAtIndex(groups, i);
        NSString *currentGroupName = (NSString*)ABRecordCopyCompositeName(currentGroup);

        if ([currentGroupName isEqualToString:GroupName])
        {
            hasGroup = YES;
            groupRecordId = ABRecordGetRecordID(currentGroup);
        }
        CFRelease(currentGroupName);
    }

    if (hasGroup == NO)
    {
        [self createNewGroup];
    }

    CFRelease(groups);
    return hasGroup;
}

Its easier and more reliable to just get the Record ID.
If found this on an unrelated post - https://stackoverflow.com/a/8249975/874927

- (BOOL)checkIfGroupExists
{   
    BOOL hasGroup = NO;

    CFIndex groupCount = ABAddressBookGetGroupCount(addressBook);
    CFArrayRef groups = ABAddressBookCopyArrayOfAllGroups(addressBook);

    for (int i = 0; i < groupCount; i++)
    {
        ABRecordRef currentGroup = CFArrayGetValueAtIndex(groups, i);
        NSString *currentGroupName = (NSString*)ABRecordCopyCompositeName(currentGroup);

        if ([currentGroupName isEqualToString:GroupName])
        {
            hasGroup = YES;
            groupRecordId = ABRecordGetRecordID(currentGroup);
        }
        CFRelease(currentGroupName);
    }

    if (hasGroup == NO)
    {
        [self createNewGroup];
    }

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