1

次のコードで使用している述語について質問があります

   NSMutableArray *records = (__bridge NSMutableArray *)ABAddressBookCopyArrayOfAllPeople( addressBook );

    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"record.phoneNumber contains %@",@"123"]; 


    @try {
            [records filterUsingPredicate:predicate];
    }
    @catch (NSException *exception) {
        NSLog(@"%@",exception);
    }
    @finally {
        //
    }

私が得る例外は次のとおりです。

[<__NSCFType 0x6e2c5e0> valueForUndefinedKey:]: このクラスは、キー レコードのキー値コーディングに準拠していません。

アドレス帳の述語に関するガイドを見つけようとしましたが、うまくいきませんでした。助言がありますか?

4

1 に答える 1

1

を使用してアドレス帳をフィルタリングすることはできませんNSPredicates。さらに、 phoneNumber は のフィールドではありませんABRecordRef。ユーザーは複数の電話番号を持つことができるため、それぞれを検査する必要があります。

次のようにします。

CFArrayRef people = ABAddressBookCopyArrayOfAllPeople(addressBook);

NSMutableArray *matchingPeople = [NSMutableArray array];
for (CFIndex i = 0; i < CFArrayGetCount(people); i++) {
    ABRecordRef person = CFArrayGetValueAtIndex(people, i);
    ABMultiValueRef phones = ABRecordCopyValue(person, kABPersonPhoneProperty);
    int phoneNumbers = ABMultiValueGetCount(phones);
    if (phoneNumbers > 0) {
        for (CFIndex i = 0; i < phoneNumbers; i++) {
            NSString *phone = (NSString *)CFBridgingRelease(ABMultiValueCopyValueAtIndex(phones, i));
            if ([phone rangeOfString:@"123"].location != NSNotFound) {
                [matchingPeople addObject:person];
                break;
            }
        }
    }
    CFRelease(phones);
}
CFRelease(people);

個人的には、ABRecordRefs を配列に追加しません。レコードから必要なフィールドだけを含む値オブジェクトを作成し、それを追加します。ループが完了したら、追加しないようにします。ぶら下がっている CFType はありません。

于 2012-11-29T22:12:00.200 に答える