xcode 如何将 ABRecordRef 添加到 iPhone 中的 NSMutableArray?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6071256/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 21:05:00  来源:igfitidea点击:

how can I Add a ABRecordRef to a NSMutableArray in iPhone?

iphoneobjective-cxcodensmutablearrayaddressbook

提问by Hadu

I want to create an array of ABRecordRef(s) to store contacts which have a valid birthday field.

我想创建一个 ABRecordRef(s) 数组来存储具有有效生日字段的联系人。

   NSMutableArray* bContacts = [[NSMutableArray alloc] init];
   ABAddressBookRef addressBook = ABAddressBookCreate();
   CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBook);
   CFIndex nPeople = ABAddressBookGetPersonCount(addressBook);

   for( int i = 0 ; i < nPeople ; i++ )
   {
       ABRecordRef ref = CFArrayGetValueAtIndex(allPeople, i );
       NSDate* birthdayDate = (NSDate*) ABRecordCopyValue(ref, kABPersonBirthdayProperty);
       if (birthdayDate != nil){
           [bContacts addObject:ref];
       }
   }

The compiler shows this warning: warning: passing argument 1 of 'addObject:' discards qualifiers from pointer target typeI searched the web and found I have to cast ABRecordRef to a ABRecord* to be able to store in a NSMutableArray.

编译器显示此警告: 警告:传递参数 1 of 'addObject:' 丢弃来自指针目标类型的限定符我在网上搜索,发现我必须将 ABRecordRef 转换为 ABRecord* 才能存储在 NSMutableArray 中。

[bContacts addObject:(ABRecord*) ref];

But it seems ABRecord is not part of iOS frameworks. Now how I store ABRecordRef to NSMutableArray?

但似乎 ABRecord 不是 iOS 框架的一部分。现在我如何将 ABRecordRef 存储到 NSMutableArray?

采纳答案by Felix

[bContacts addObject:(id) ref];

回答by DarkDust

A ABRecordRefis a typedef for CFTypeRefand that in turn resolves to const void *. And this is where the warning comes from: with the call to addObject:, the constqualifier is "lost".

AABRecordRef是一个 typedef for CFTypeRef,然后解析为const void *. 这就是警告的来源:调用 时addObject:const限定符“丢失”。

In this case we know it's OK. A CFTypeRef is a semi-highlevel type, instances of this type support CFRetainand CFRelease. That in turn means it's probably OK to cast it to idand treat it as a NSObject. So you should be simply able to do:

在这种情况下,我们知道没问题。CFTypeRef 是一种半高级类型,这种类型的实例支持CFRetainCFRelease. 这反过来意味着可以id将其转换为 NSObject 并将其视为 NSObject。所以你应该能够简单地做到:

[bContacts addObject:(id)ref];

回答by klefevre

Create an NSObject wich store your ABRecordRef like this :

像这样创建一个 NSObject 来存储你的 ABRecordRef:

// ABContact.h
@interface ABContact : NSObject
{
    ABRecordRef _record;
}

@property (nonatomic, readonly) NSDate *birthday;

- (id)initWithRecord:(ABRecordRef)aRecord;

@end

// ABContact.m
@implementation ABContact

#pragma mark - Init

- (id)initWithRecord:(ABRecordRef)aRecord;
{
    if ((self = [super init]))
        _record = CFRetain(aRecord);
    return self;
}


#pragma mark - Getter

- (NSDate *)birthday
{
    return (NSDate *)ABRecordCopyValue(record, kABPersonBirthdayProperty) autorelease];
}


#pragma mark - Memory management

- (void)dealloc
{
    CFRelease(_record);
    [super dealloc];
}

@end


You should take a look at the Erica Sadum library the author of the iPhone cookbook. Here is the code wich inspire this code -> url


您应该查看 iPhone 食谱的作者 Erica Sadum 库。这是激发此代码的代码-> url

回答by Dennis

I know this is an old question, but since the introduction of ARC, this is handled in a different way.

我知道这是一个老问题,但自从引入 ARC以来,这以不同的方式处理。

There are two possible ways of adding an ABRecordRefto an NSMutableArraynow, both of which require a bridged cast:

有两种可能的方法将 an 添加ABRecordRefNSMutableArraynow,这两种方法都需要桥接转换:

Direct conversion

直接转换

[bContacts addObject:(__bridge id)(ref)];

This simply converts the ABRecordRefpointer, and should be used, if you didn't create refyourself using a method that has Createin its name.

这只是转换ABRecordRef指针,如果您没有ref使用Create名称中包含的方法创建自己,则应该使用它。

Transfer ownership to ARC

将所有权转让给 ARC

[bContacts addObject:CFBridgingRelease(ref)];

This converts the ABRecordRefpointer, and in addition transfers its ownership to ARC. This should be used, if you used e.g. ABPersonCreate()to create a new person.

这将转换ABRecordRef指针,此外还将其所有权转移给 ARC。如果您使用例如ABPersonCreate()创建一个新人,则应该使用它。

More information can be found in the Transitioning to ARC Release Notes. There are also many other informative sources here on Stack Overflow, such as this question.

更多信息可以在过渡到 ARC 发行说明中找到。Stack Overflow 上还有许多其他信息来源,例如这个问题