- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person
property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier {
if (property == kABPersonPhoneProperty) {
ABMultiValueRef multiPhones = ABRecordCopyValue(person, kABPersonPhoneProperty);
for(CFIndex i = 0; i < ABMultiValueGetCount(multiPhones); i++) {
if(identifier == ABMultiValueGetIdentifierAtIndex (multiPhones, i)) {
CFStringRef phoneNumberRef = ABMultiValueCopyValueAtIndex(multiPhones, i);
CFRelease(multiPhones);
NSString *phoneNumber = (NSString *) phoneNumberRef;
CFRelease(phoneNumberRef);
RecipientContact *recipient = [[RecipientContact alloc] init];
recipient.phoneNumber = [NSString stringWithFormat:@"%@", phoneNumber];
recipient.name = nil;
[recipients addObject:recipient];
[recipient release];
[phoneNumber release];
}
}
}
[self dismissModalViewControllerAnimated:YES];
[self _addRecipients];
return NO;
}
- (void)_addRecipients {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSString *names = [[NSString alloc] init];
for (RecipientContact *recipient in recipients) {
names = [names stringByAppendingString:recipient.phoneNumber];
names = [names stringByAppendingString:@";"];
}
contactsField.text = names;
[pool release];
}
我正在尝试在我的项目中使用this代码,但是当我两次进入地址簿时它会崩溃。我发现当我发布一个multiPhones和phoneNumberRef时,它很顺利。但是当我CF发布他们两个,应用程序将崩溃 我已经看过Core Foundation命名约定,它说“如果一个函数名称包含”Create“或”Copy“这个词,你就拥有了这个对象。”,但为什么我发布这两个应用程序崩溃了,谢谢你。
答案 0 :(得分:2)
问题是你正在使用免费桥接然后释放NSString
NSString *phoneNumber = (NSString *) phoneNumberRef;
CFRelease(phoneNumberRef);
...
[phoneNumber release]; //<-- this is like calling CFRelease(phoneNumberRef); again
在[phoneNumber retain]
之前致电CFRelease(phoneNumberRef);
或在CFRelease
之前致电phoneNumberRef
。