代码之家  ›  专栏  ›  技术社区  ›  Zebs

从de-addressbook使用cfstrings时的内存管理

  •  0
  • Zebs  · 技术社区  · 15 年前

    我正在使用苹果提供的API读取通讯簿中的记录。

    我仍在思考内存管理等问题。 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);
    

    在这之后,我使用这些值,唯一的事情是我不知道是否应该释放它们。

    我会很感激这样一个答案,它也能帮助我更好地理解记忆管理,或者为我指明正确的方向。

    谢谢您!

    1 回复  |  直到 13 年前
        1
  •  5
  •   Justin Spahr-Summers    15 年前

    核心基础的经验法则是任何功能包括 Copy 或 Create 以它们的名义返回您负责释放的对象。苹果的 Memory Management Guide for Core Foundation 详细解释一下。

    推荐文章