1

我们如何存储和访问一个列表,ABRecordRef以便我们可以在另一个类和以后使用它?

4

2 回答 2

2

您可以从 addressBook中获取recordId类型的记录,然后将其转换为. 将其存放在ABRecordRefNSStringNSUserDefaults

当你想获取记录时......

recordId从中获取NSUserDefaults,将其转换为整数

然后使用

    ABRecordRef myRecord =
 ABAddressBookGetPersonWithRecordID(addressBook, recordId);

所以你会得到你之前保存的记录。

于 2011-08-09T12:31:00.707 回答
1

试试这个:

ABAddressBookRef addressBook = ABAddressBookCreate();
ABRecordRef person = ABRecordGetRecordID(1);  // Or whatever you're using to get the record.

// do this for each piece of information you need:
NSString *firstName = (NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty);
NSString *lastName = (NSString *)ABRecordCopyValue(person, kABPersonLastNameProperty);

// Get your receiving dict ready:
NSMutableDictionary *personDict = [NSMutableDictionary dictionary];

// then test them for a value (don't want nil values in the dictionary)
// Add each value as you test it.
if (firstName) {
    [personDict setObject:firstName forKey:@"FistName"];
}
if (lastName) {
    [personDict setObject:lastName forKey:@"LastName"];
}

// Add the personDict to NSUserDefaults:
[[NSUserDefaults standardDefaults] setObject:personDict forKey:@"MyPersonsDictionary"];

// Clean up after yourself:
[firstName release];
[lastNmae release];

应该是这样的。

于 2011-08-08T13:08:38.123 回答