2010-11-22 59 views
0

我正在使用Apple提供的API從AddressBook中讀取記錄。使用CFStrings從地址簿進行內存管理AddressBook

我仍然對內存管理很感興趣,因此CFStrings目前令我困惑。

這是我如何得到屬性:

//Get Basic properties 
NSString* firstName = (NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty); 
NSString* lastName = (NSString *)ABRecordCopyValue(person, kABPersonLastNameProperty); 
NSNumber* record = [NSNumber numberWithInt:ABRecordGetRecordID(person)]; 

//Build Full Name 
NSString* fullName=[self fullNameWith:firstName and:lastName]; 

//Get Phone number and Label 
ABMultiValueRef phone = ABRecordCopyValue(person, property); 
    //Turn identifier into index 
CFIndex index = ABMultiValueGetIndexForIdentifier(phone, identifier); 
    //Get the value and the label using the index 
NSString *value =(NSString *)ABMultiValueCopyValueAtIndex(phone, index); 
CFStringRef label = ABMultiValueCopyLabelAtIndex(phone, index); 
    //Get the localized value of hte label 
NSString * localizedLabel = (NSString *)ABAddressBookCopyLocalizedLabel(label); 

之後,我使用的值,唯一的事情是,我不知道我是否應該釋放它們。

我將不勝感激的答案,也幫助我更好地理解內存管理或指出我正確的方向。

謝謝!

回答

5

Core Foundation的經驗法則是任何包含CopyCreate的函數都會返回一個您負責釋放的對象。 Apple的Memory Management Guide for Core Foundation更詳細地解釋了這一點。

+0

謝謝!只是爲了澄清,當你從CFString轉換到NSString時,你會[字符串發佈]還是CFRelease(字符串)? – Zebs 2010-11-22 04:25:30

+0

實際上,在這種情況下,它們的行爲完全相同,因爲CFString和NSString是「免費橋接的」。我會使用最清晰,最容易理解的代碼以及可能閱讀它的人。 – 2010-11-22 04:27:40