2010-09-15 62 views
0

我想使用PeoplePicker來檢索聯繫人的名稱和地址,並將其存儲到NSUserDefaults中,我最終希望在桌面視圖中檢索它。如何將從地址簿檢索到的信息保存到NSUserDefaults?

我的問題是如何將信息保存在NSUserDefaults中。 我已經使用NSDictionary,但我沒有取得任何進展。 所以我試圖將數組保存到NSUserDefaults。

有人可以幫助我嗎?

我的代碼如下所示:

- (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; 
} 

回答

0
[locatie setObject:yourObject forKey:@"YoureKey"]; 

如果你想存儲ADRESS使用:

[locatie setObject:adress forKey:@"adressKey"]; 

以檢索使用:

adress = [locatie valueForKey:@"adressKey"]; 

注: 在這些例子中catie是:NSUserDefaults *locatie = [NSUserDefaults standardUserDefaults];

+0

我想選擇一個地址,然後用我選擇的地址填充數組,並且已經保存了它。我不知道該怎麼做。 – Nico 2010-09-15 09:25:35

+0

你是什麼意思?如果你想保存/從NSUserDefaults使用我上面提到的..如果你想添加一個對象到數組使用addObject:像這樣: [youreArray addObject:adress]; – LarsJK 2010-09-15 10:28:48

+0

你可以在我的代碼中看到我試圖從通訊錄中檢索數據並將其添加到NSArray中。現在我正在使用[locationArray addObjectsFromArray:theArray]; (@「%@」,locationArray); \t \t \t \t \t \t * NSUserDefaults的地區= [NSUserDefaults的standardUserDefaults]; \t \t \t NSData * data = [NSKeyedArchiver archivedDataWithRootObject:locationArray]; NSLog(@「%@」,data); \t \t \t [locatie setObject:data forKey:@「array」]; \t \t \t [locatie synchronize]; 將nsarray添加到nsmutablearray並將其作爲NSdata添加到nsuserdefaults。我無法檢索數據,我收到一個(null) – Nico 2010-09-15 11:57:49