0

Appleが提供するAPIを使用してAddressBookからレコードを読み取ります。アドレス帳のCFStringを使用するときのメモリ管理

私はまだメモリ管理について頭を悩ましています。そのため、現時点では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

コアファンダメンタルズの経験則では、名前にCopyまたはCreateを含む関数はすべて、あなたがリリースするオブジェクトを返します。 AppleのMemory Management Guide for Core Foundationではこれについてもう少し詳しく説明しています。

+0

ありがとうございました! CFStringからNSStringへのキャストを行うときに[string release]またはCFRelease(string)を実行するかどうかを明確にするだけですか? – Zebs

+0

実際には、このケースでは、CFStringとNSStringが「フリーダイヤル」のため、まったく同じように動作します。私は、あなたのコードとそれを読む人が誰にとっても分かりやすく、最も簡単に理解できるものを使用します。 –