IPhone AddressBook 方法返回或创建 ABRecord
我的地址簿和内存确实有问题。我希望它返回一条记录(现有记录或新记录),我可以在其中保存联系人。根据构建和分析工具,这个函数有很多错误。我已经阅读了文档并重写了很多次,但我似乎无法正确理解。
编写这个方法的正确方法是什么? / 我做错了什么?
//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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只获取记录 ID 更容易、更可靠。
如果在不相关的帖子中发现此内容 - https://stackoverflow.com/a/8249975/874927
Its easier and more reliable to just get the Record ID.
If found this on an unrelated post - https://stackoverflow.com/a/8249975/874927