我在AddressBook和内存方面遇到了真正的问题。我希望它返回一个可以保存联系人的记录(现有记录或新记录)。根据构建和分析工具,这个函数有很多错误。我已经阅读了文档并重写了很多次,但我似乎不能正确地理解它。
写这个方法的正确方法是什么?/我做错了什么?
//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;}
发布于 2012-02-21 00:00:03
只获取记录ID会更简单、更可靠。如果在一个无关的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;
}https://stackoverflow.com/questions/9361150
复制相似问题