I’ve been stumped with this for the last few days, I’m a bit new to objective-c.
I have up to 3 NSDictionary objects saved in the NSUserDefaults with keys “one”, “two”, and “three” (All contents of the dictionaries are NSStrings, so it should be ok that they’re saved in the NSUserDefaults).
I am trying to load these three NSDictionaries into a NSMutableDictionary when a view apears, but for some reason they’re not being added in.
@synthesize serverDict;
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[serverDict removeAllObjects];
NSDictionary *favs;
NSArray *serverList = [NSArray arrayWithObjects:@"one",@"two",@"three", nil];
for (id server in serverList) {
favs = [[NSUserDefaults standardUserDefaults] dictionaryForKey:server];
if ((favs != nil) && ([favs count] != 0)) {
[serverDict setObject:favs forKey:server];
NSLog(@"server:%@, objects in server:%d",server, [favs count]);
//This outputs:
//"server:one, objects in server:2"
//"server:three, objects in server:2"
}
}
NSLog(@"[serverDict count] = %d", [serverDict count]);
//This outputs:
//"[serverDict count] = 0"
for (id key in serverDict) {
NSLog(@"serverDict key: %@, value: %@", key, [serverDict objectForKey:key]);
//No Output here
}
}
This is in the .h file
@interface FavoritesViewController : UITableViewController <UITableViewDelegate, UITableViewDataSource> {
...
NSMutableDictionary *serverDict;
}
@property (nonatomic, retain) NSMutableDictionary *serverDict;
@end
From the comments (NSLogs), we can see that the dictionaries exist in the NSUserDefaults, but they’re not being added into serverDict.
Any help would be appreciated!! I’m still in the learning phase of objective-c…
Thanks!!
You need to use self.serverDict everywhere instead of serverDict except when releasing it in dealloc as it is defined as a property. Also make sure you alloc and init self.serverDict before you use it (I’m assuming you already are, but not using self.serverDict).