使用 AddressBook 中的 CFString 时的内存管理

发布于 2024-10-03 20:34:30 字数 1086 浏览 0 评论 0原文

我正在使用 Apple 提供的 API 从 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);

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

我希望得到一个能够帮助我更好地理解内存管理或者为我指明正确方向的答案。

谢谢你!

I am reading records from the AddressBook using the Apple provided API.

I am still getting my head around memory management and so CFStrings are confusing me at the moment.

This is how I am getting the properties:

//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);

After that I use the values, the only thing is that I dont know if I should release them or not.

I would appreciate an answer that also helped me understand memory management better or that points me to the right direction.

Thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

缱倦旧时光 2024-10-10 20:34:30

Core Foundation 的经验法则是,名称中包含 CopyCreate 的任何函数都将返回您负责释放的对象。苹果的

The rule of thumb for Core Foundation is that any functions that include Copy or Create in their name will return an object that you are responsible for releasing. Apple's Memory Management Guide for Core Foundation explains this in a bit more detail.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文