使用 AddressBook 中的 CFString 时的内存管理
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Core Foundation 的经验法则是,名称中包含
Copy
或Create
的任何函数都将返回您负责释放的对象。苹果的The rule of thumb for Core Foundation is that any functions that include
Copy
orCreate
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.