简体   繁体   English

如何在iOS中将NSNumber转换为ABRecordID

[英]How would I bridge cast NSNumber to ABRecordID in iOS

I start learning programming with Objective-C, and do not have much knowledge of C. So, bridge casting in iOS 6 is still somewhat confusing for me. 我开始用Objective-C学习编程,并且对C语言知之甚少。因此,iOS 6中的桥接构建对我来说仍然有点混乱。

Here is the scenario: 这是场景:

I have a person ABRecordID stored in CoreData as 'NSNumber' attribute. 我将一个人ABRecordID存储在CoreData中作为'NSNumber'属性。 Later on, I would like to access to that person directly, so I want to use the person's ABRecordID to access the contact info using ABAddressBook. 稍后,我想直接访问该人,因此我想使用该人的ABRecordID来使用ABAddressBook访问该联系人信息。 Noticed that ABAddressBookGetPersonWithRecordID needs ABRecordID, below is how I perform casting in my code... 注意到ABAddressBookGetPersonWithRecordID需要ABRecordID,下面是我在代码中执行转换的方法......

address_book = ABAddressBookCreate();
ABRecordID rec_id = (__bridge ABRecordID)person.record_id;

However, This was not successful and I was given incompatible types casting 'int' into 'ABRecordID' (aka 'int') with a __bridge cast . 但是,这没有成功,我被赋予了incompatible types casting 'int' into 'ABRecordID' (aka 'int') with a __bridge cast

Already confused as it is, what would be a proper way to bridge cast between ARC type and CF type? 已经混淆了,在ARC类型和CF类型之间桥接转换的正确方法是什么?

Also, in which case should I use (__bridge retained) instead of (__bridge) ? 另外,在哪种情况下我应该使用(__bridge retained)而不是(__bridge)

The ABRecordID is a synonym (typedef) for int32_t which is a 32-bit integer. ABRecordIDint32_t的同义词(typedef),它是一个32位整数。 So typecasting is not the proper approach. 因此,类型转换不是正确的方法。 You want to create an NSNumber with the value of the id. 您想要使用id的值创建NSNumber

ABRecordId rec_id = person.record_id;
NSNumber *wrapper = [NSNumber numberWithInt:(int)rec_id];

and later: 然后:

NSNumber *wrapper = ...
ABRecordId rec_id = (ABRecordId)[wrapper intValue];

Please note that bridging and ARC are irrelevant when going to/from ABRecordId and NSNumber. 请注意,往返ABRecordId和NSNumber时,桥接和ARC无关紧要。 There is no use of any of the CF types in this case. 在这种情况下,没有使用任何CF类型。 You'll know when you are using CF types because you'll have a variable whose type begins with CF (such as CFArrayRef ). 您将知道何时使用CF类型,因为您将拥有一个类型以CF开头的变量(例如CFArrayRef )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM