-1

Icstored the value of 2 label in NSUserDefault, trought NSMutableArray.

NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];

// Add a bookmark
NSMutableDictionary *bookmark = [NSMutableDictionary new];
[bookmark setValue:author.text forKey:@"author"];
[bookmark setValue:book.text forKey:@"book"];
[bookmarks addObject:bookmark];

// Save your (updated) bookmarks
[userDefaults setObject:bookmarks forKey:@"bookmarks"];
[userDefaults synchronize];

Now my problem is how retrieve the values of NSUserDefault trought NSArray?

 NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
 self.dataArray = [prefs arrayForKey:@"bookmarks"];

 NSString *author =  ?? 
 NSString *book =  ??
Maurizio
  • 89
  • 2
  • 15
  • -1. Improper naming of objects. RTM. –  Mar 08 '12 at 09:10
  • You can't add NSArray to NSUserDefaults. See this post [http://stackoverflow.com/questions/537044/storing-custom-objects-in-an-nsmutablearray-in-nsuserdefaults][1] [1]: http://stackoverflow.com/questions/537044/storing-custom-objects-in-an-nsmutablearray-in-nsuserdefaults – rakeshNS Mar 08 '12 at 17:27

3 Answers3

1
NSString * author = [self.dataArray objectForKey: @"author"];
NSString * book = [self.dataArray objectForKey: @"book"];

This assumes you successfully saved the array and that you successfully re-loaded the array from NSUserDefaults. You really should do some error checking in your code.

Michael Dautermann
  • 88,797
  • 17
  • 166
  • 215
1

In the dataArray, you can store lots of bookmarks.This methods can log all of the bookmarks.

NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];  
self.dataArray = [prefs objectForKey:@"bookmarks"]; 

for(int i=0;i<[dataArray count];i++){
    NSDictionary *dic = [dataArray objectAtIndex:i];
    NSLog(@"%@",dic);
    NSString * author = [dic objectForKey: @"author"];
    NSString * book = [dic objectForKey: @"book"];  
}
Dzy
  • 153
  • 4
0

you have inserted an array with a dictionary. you can retrieve it by using

self.dataArray = [prefs arrayForKey:@"bookmarks"];
 NSString *author =  [self.dataArray objectAtIndex:0] objectForKey:@"author"]; 
 NSString *book =   [self.dataArray objectAtIndex:0] objectForKey:@"book"];

But it is better to store the dictionary directly in user defaults.

Vignesh
  • 10,205
  • 2
  • 35
  • 73