Iphone 如何将从通讯簿检索到的信息保存到NSUserDefault?
我试图使用PeoplePicker检索联系人的姓名和地址,并将其存储到NSUserDefaults中,最终我希望在tableview中检索它 我的问题是如何将信息保存在NSUserDefaults中。 我用过这本字典,但没有任何进展。 因此,我试图将数组保存到NSUserDefaults 有人能帮我吗 我的代码如下所示:Iphone 如何将从通讯簿检索到的信息保存到NSUserDefault?,iphone,objective-c,xcode,Iphone,Objective C,Xcode,我试图使用PeoplePicker检索联系人的姓名和地址,并将其存储到NSUserDefaults中,最终我希望在tableview中检索它 我的问题是如何将信息保存在NSUserDefaults中。 我用过这本字典,但没有任何进展。 因此,我试图将数组保存到NSUserDefaults 有人能帮我吗 我的代码如下所示: - (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePic
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker
shouldContinueAfterSelectingPerson:(ABRecordRef)person
property:(ABPropertyID)property
identifier:(ABMultiValueIdentifier)identifier {
// Only inspect the value if it's an address.
if (property == kABPersonAddressProperty) {
ABMutableMultiValueRef multiValue = ABRecordCopyValue(person, property);
for(CFIndex i=0;i<ABMultiValueGetCount(multiValue);i++)
{
ABMultiValueRef multi = ABRecordCopyValue(person, property);
// Set up an NSArray and copy the values in.
NSArray *theArray = [(id)ABMultiValueCopyArrayOfAllValues(multi) autorelease];
// Figure out which values we want and store the index.
const NSUInteger theIndex = ABMultiValueGetIndexForIdentifier(multi, identifier);
// Set up an NSDictionary to hold the contents of the array.
NSDictionary *dictionary = [theArray objectAtIndex:theIndex];
// Set up NSStrings to hold keys and values. First, how many are there?
const NSUInteger theCount = [dictionary count];
NSString *keys[theCount];
NSString *values[theCount];
// Get the keys and values from the CFDictionary. Note that because
// we're using the "GetKeysAndValues" function, you don't need to
// release keys or values. It's the "Get Rule" and only applies to
// CoreFoundation objects.
[dictionary getObjects:values andKeys:keys];
// Set the address label's text.
NSString *address;
address = [NSString stringWithFormat:@"%@, %@, %@",
[dictionary objectForKey:(NSString *)kABPersonAddressStreetKey],
[dictionary objectForKey:(NSString *)kABPersonAddressCityKey],
[dictionary objectForKey:(NSString *)kABPersonAddressCountryKey]];
NSLog(@"%@", address);
[self dismissModalViewControllerAnimated:YES];
[self.navigationController popViewControllerAnimated:YES];
}
NSUserDefaults *locatie = [NSUserDefaults standardUserDefaults];
CFRelease(multiValue);
}
return NO;
}
-(BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController*)peoplePicker
选择人员后应继续:(ABRecordRef)人员
属性:(ABPropertyID)属性
标识符:(ABMultiValueIdentifier)标识符{
//仅当值是地址时才检查该值。
如果(属性==KabPersonalAddressProperty){
ABMutableMultiValueRef multiValue=ABRecordCopyValue(个人、财产);
对于(cfi=0;i
如果要存储地址,请使用:
[locatie setObject:adress forKey:@"adressKey"];
要重新使用,请执行以下操作:
adress = [locatie valueForKey:@"adressKey"];
注:
在这些示例中,位置是:NSUserDefaults*locatie=[NSUserDefaults standardUserDefaults];
我想选择一个地址,然后用我选择并已保存的地址填充数组。我不知道怎么做。你是什么意思?如果你想保存/检索NSUserDefault,请使用我上面提到的方法。如果你想向数组添加对象,请使用addObject:like:[YourArray addObject:address];正如您在我的代码中看到的,我正在尝试从通讯簿检索数据并将其添加到NSArray中。现在我使用的是[locationArray addObjectsFromArray:theArray];NSLog(@“%@”,locationArray);NSUserDefaults*locatie=[NSUserDefaults standardUserDefaults];NSData*data=[NSKeyedArchivedDataWithRootObject:locationArray];NSLog(@“%@”,data);[locatie setObject:data forKey:@“array”];[locatie synchronize];将nsarray添加到nsmutablearray并将其作为NSdata添加到nsuserdefaults中。我无法检索数据,我收到一个(null)您需要使用NSKeyedArchiver吗?如何使用[locatie setObject:locationArray forKey:@“array”];有效吗?我已经尝试了你给我的代码,但我无法将数组保存到另一个数组中。因此,现在我必须先弄清楚这一点,然后才能尝试将数组保存到NSUserDefaults中
adress = [locatie valueForKey:@"adressKey"];