Storing dictionary of dictionaries in NSUserDefaults - objective-c

I am trying to store a dictionary which contains two dictionaries in NSUserDefaults.
NSUserDefaults *def = [NSUserDefaults standardUserDefaults];
NSDictionary *defDict = [def dictionaryRepresentation];
defaultTopics = [[NSArray alloc] initWithObjects:#"Animals",
#"Numbers",
#"AroundTown",
#"Actions", nil];
defaultValues = [[NSArray alloc] initWithObjects:#1, #0, #0, #0, nil];
NSDictionary *dd = [[NSDictionary alloc] initWithObjects:defaultValues forKeys:defaultTopics];
NSDictionary *de = [[NSDictionary alloc] initWithObjects:#[dd, dd] forKeys:#[#"English", #"Indonesian"]];
[defDict setValue:de forKey:#"UnlockedTopics"];
[def synchronize];
After this, NSLog(#"%#", [def dictionaryRepresentation]);, prints the correct dictionary, as long as I don't quit the app everything is as expected. But then, if I quit the app and relaunch,
[defDict objectForKey:#"UnlockedTopics"]
is always nil. What is the reason, and how to fix this?

The reason the dictionary of dictionaries is not getting stored is that you are never storing it. All you're doing is setting the key of a dictionary. You are calling
[defDict setValue:de forKey:#"UnlockedTopics"];
But that is not the same thing as setting any #"UnlockedTopics" key-value pair in NSUserDefaults. If you want to write to NSUserDefaults, write to it - e.g. by sending setObject:forKey: to NSUserDefaults.

Related

NSMutableDictionary won't save data

hope someone can help me with a problem I've been wrestling with...
Using MapBox to develop a map-based app, and I want to attach an NSMutableDictionary to each of the map annotations to store additional data. I had it working but XCode kept throwing me warning about some of my data/object types, so I went through and tidied those up, and now it's broken. The idea is that on ViewDidLoad, the program runs through a set of plist dictionaries to set up each annotation correctly - that's still running okay, because my initial anno markers pop up with their correct settings. However rather than run back to the plist every time, I want to attach a dictionary to each annotation's userinfo property, which I can then use for toggling selection data and other functions. Here's my code:
NSDictionary *ExploreSteps = [[NSDictionary alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"ExploreSteps" ofType:#"plist"]];
for (NSString *key in [ExploreSteps allKeys])
{
//Loop through keys for each anno
NSDictionary *thisStep = [ExploreSteps objectForKey:key];
NSNumber *annoIndex = [thisStep objectForKey:#"Index"];
NSNumber *isLive = [thisStep valueForKey:#"isLive"];
NSString *annoTitle = [thisStep objectForKey:#"Title"];
NSString *annoText = [thisStep objectForKey:#"Text"];
NSString *imagefile = [thisStep objectForKey:#"Imagefile"];
double longitude = [[thisStep objectForKey:#"Longitude"] doubleValue];
double latitude = [[thisStep objectForKey:#"Latitude"] doubleValue];
NSString *pagefile = [thisStep objectForKey:#"Pagefile"];
NSString *audiofile = [thisStep objectForKey:#"Audiofile"];
CLLocationCoordinate2D annoCoord = CLLocationCoordinate2DMake(latitude, longitude);
RMAnnotation *annotation = [[RMAnnotation alloc] initWithMapView:mapView coordinate:annoCoord andTitle:annoTitle];
annotation.annotationIcon = [UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForResource:imagefile ofType:#"png"]];
annotation.userInfo = [NSMutableDictionary dictionaryWithObjectsAndKeys:annoIndex, #"index", isLive, #"isLive", annoTitle, #"annoTitle", annoText, #"annoText", imagefile, #"imagefile", pagefile, #"pagefile", audiofile, #"audiofile", nil];
NSLog(#"Title: %#",[annotation.userInfo objectForKey:#"annoTitle"]);
[mapView addAnnotation:annotation];
}
The NSLog should spit out the annoTitle string, but instead it's giving me a null every time, and the behaviour of the rest of the app also shows that info stored in the dictionary simply isn't "getting through".
Any ideas? Thanks in advance!
ETA: Modified code for initializing the dictionary (not that it seems to make any difference to the problem!):
NSMutableDictionary *myUserInfo = [[NSMutableDictionary alloc] initWithObjectsAndKeys:annoIndex, #"index", isLive, #"isLive", annoTitle, #"annoTitle", annoText, #"annoText", imagefile, #"imagefile", pagefile, #"pagefile", audiofile, #"audiofile", nil];
annotation.userInfo = myUserInfo;
NSLog(#"Title: %#",[annotation.userInfo objectForKey:#"annoTitle"]);
NSLog(#"Length: %u",[[annotation.userInfo allKeys] count]);
(Title now returns "(null)", while Length returns "1", if that's at all helpful...)
Almost certainly one of your objects is nil. You mention that allKeys] count] returns 1 so I can go further and say that your value for isLive is nil. Hence your original line:
[NSMutableDictionary dictionaryWithObjectsAndKeys:annoIndex, #"index", isLive, #"isLive", annoTitle, #"annoTitle", annoText, #"annoText", imagefile, #"imagefile", pagefile, #"pagefile", audiofile, #"audiofile", nil];
Acts exactly the same as:
[NSMutableDictionary dictionaryWithObjectsAndKeys:annoIndex, #"index", nil, #"isLive", annoTitle, #"annoTitle", annoText, #"annoText", imagefile, #"imagefile", pagefile, #"pagefile", audiofile, #"audiofile", nil];
And the dictionary takes annoIndex to be the final key-value pair.
I'd suggest that probably you want to take a mutable copy of thisStep and strip out the keys you don't want, then pass it along as the userInfo.
It's the way you are creating the NSMutableDictionary for userInfo. Take a look at this Difference between [[NSMutableDictionary alloc] initWithObjects:...] and [NSMutableDictionary dictionaryWithObjects:...]?
"
+dictionaryWithObjects: returns an autoreleased dictionary
-initWithObjects: you must release yourself
if you want the dictionary to persist as a instance variable, you should create it with an init method or retain an autoreleased version, either way you should be sure to release it in your dealloc method
"

iphone app memory management in save/restore

I am confused by the memory management in this scenario.
In my app user makes periodic input inside UITextField tf and the typed strings (NSString*) are stored as elements of a MSMutableArray *arr through addObject. The stored collection is displayed inside a UITableView. My app can go into bkgr and is periodically awakened by push notifications. As I understand it, the data stored in arr can be lost while my app is non-active and, to preserve it, I need to do archive/restore.
My archive/restore are using
NSUserDefaults*prefs;
[prefs setObjectForKey:x forKey:key]
to archive and
[prefs objectForKey:key]
to restore every item of arr.
Question1: I think that to prevent the memory leak I need to do [arr release]
Do I also need to do a release on every object which I have added to arr or, since I did not allocate the NSString for tf, it will be done for me automatically?
Question2: in restore I start with something like arr=[[NSMutableArray alloc] initWithObjects:nil]; before I can read and add archived items back to arr. I think that [prefs objectForKey:key] is released as soon as I leave the scope in which it was read - thus I need something like retain to keep it in arr. Would this schema work in the next archive/restore cycle due to another app deep sleep?
Is there a cleaner way of achieving the same?
Thanks.
Victor
Adding objects to an NSArray causes the NSArray to retain each object.
So in a case where you are instantiating objects, then adding them to an array, those objects do not need to be further retained:
// saving strings inside an array, then array to the NSUserDefaults
NSString *string1 = #"My String 1";
NSString *string2 = #"My String 1";
NSMutableArray *arr = [[NSMutableArray alloc] initWithCapacity:10];
[arr addObject:string1];
[arr addObject:string2];
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];;
[prefs setObject:arr forKey:#"MyArray"];
[arr release];
Then to restore the entire array from prefs:
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSArray *array = [prefs objectForKey:#"MyArray"];
Alternately, to save strings under separate keys, it would be something like this:
[prefs setObject:[arr objectAtIndex:0] forKey:#"MyFirstStringKey"];
[prefs setObject:[arr objectAtIndex:1] forKey:#"MySecondStringKey"];
For the restore, you will also just add the items to the array, no retain required:
// assuming this time several keys added to an array
// also note using autoreleased version of array - much easier
NSMutableArray *arr = [NSMutableArray arrayWithCapacity:10];
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];;
[arr addObject:[prefs objectForKey:#"MyFirstStringKey"]];
[arr addObject:[prefs objectForKey:#"MySecondStringKey"]];
// then assign arr or use it otherwise
Also easier still is to use a non-mutable array and instantiate the array with the list of objects you want to have on the array:
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];;
NSArray *arr = [NSArray arrayWithObjects:[prefs objectForKey:#"MyFirstStringKey"], [prefs objectForKey:#"MySecondStringKey"], nil];

Change from a NSMutableArray to a .plist

I have been working through several tutorials on uitableviews.
I have put, as instructed, all the info into a 'listofitems' as below
listOfItems = [[NSMutableArray alloc] init];
NSArray *countriesToLiveInArray = [NSArray arrayWithObjects:#"Iceland", #"Greenland", #"Switzerland", #"Norway", #"New Zealand", #"Greece", #"Rome", #"Ireland", nil];
NSDictionary *countriesToLiveInDict = [NSDictionary dictionaryWithObject:countriesToLiveInArray forKey:#"Countries"];
NSArray *countriesLivedInArray = [NSArray arrayWithObjects:#"India", #"U.S.A", nil];
NSDictionary *countriesLivedInDict = [NSDictionary dictionaryWithObject:countriesLivedInArray forKey:#"Countries"];
[listOfItems addObject:countriesToLiveInDict];
[listOfItems addObject:countriesLivedInDict];
This creates a sectioned table view. I would like to know how to change it into a .plist instead of typing it all out into the RootViewController.m. I would still like it to be in a sectioned tableview.
Is there a simple method for changing from this NSMutableArray,NSArray and NSDictionary to a plist?
There's a simple method for this writeToFile:atomically::
[listOfItems writeToFile:destinationPath atomically:YES];
This will automatically create a file with plist inside it.
that sorta depends on what you want in a plist, and what you put into it. if the entries and contents are all CFPropertyList types (CFString,CFDate,CFData,CFDictionary,CFArray,CFNumber...) then just create it with something like CFPropertyListCreateDeepCopy.
if you have non-convertible custom objects (e.g., your own NSObject subclasses), then see the cocoa archiving topics.
This is the simple function end hear relization
This is function is updating NSArray
- (void) WriteRecordToFile:(NSMutableDictionary*)countDict {
// Here to write to file
databasePathCallCount = #"plist path";
NSMutableArray *countArray = [NSMutableArray arrayWithContentsOfFile:databasePathCallCount];
if(countArray)
[countArray addObject:countDict];
[countArray writeToFile:databasePathCallCount atomically:NO];
}

NSDictionary into a NSInputStream

Is it possible to put in my NSDictionary into a NSInputStream?
This would be my NSDictionary
NSArray *keys = [NSArray arrayWithObjects:#"name", #"device_token", #"identifier", nil];
NSArray *values = [NSArray arrayWithObjects:#"Test iPhone", initDeviceToken, [UIDevice currentDevice].uniqueIdentifier, nil];
NSDictionary *parameters = [NSDictionary dictionaryWithObjects:values forKeys:keys];
I tried something like this but it didn't worked...
NSInputStream *inputStream = [[NSInputStream alloc] init];
[inputStream setValue:#"123" forKey:#"identifier"];
Thx a lot
Sure. The way you can do it is serialize your dictionary into an NSData object using NSPropertyListSerialization, and then write your data object out to your NSInputStream.
The downside is that this requires that your dictionary only hold plist objects (numbers, strings, dates, data, arrays, and dictionaries).
Yes.
Any object that conforms to the NSCoding protocol is serializable and as such writable to a stream. Of course the items in a container object must also conform to the NSCoding protocol.

Plists and connections

I have an app that needs to connect and receive data, different each time that you click in one tab.
Then to show the data to the user, i use a "element.plist" where i have one array of dictionaries( each dictionary has the info in different strings: name, category, ...). I load the info from this plist.
I would like then, to continue using the same structure. Each time i receive the connection data:
delete the content in the plist
save the new content (I can do this in the parser method, each time that i have one object with all the information)
Read the info like i'm doing now.
The step that i can't do is the second.
thanks
I'm not sure I completely understand your question,
but I'll try to help.
below is some apple sample code that saves a plist when an application is exiting.
the second line sets the name of the plist file:
NSString *bundlePath = the application directory + "Data"
the third line defines a dictionary with all the data to be saves:
NSDictionary *plistDict
the fourth line formats this dictionary as property list data:
NSData *plistData
which then gets saved as Data.plist
- (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender
{
NSString *errorDesc;
NSString *bundlePath = [[NSBundle mainBundle] pathForResource:#"Data" ofType:#"plist"];
NSDictionary *plistDict = [NSDictionary dictionaryWithObjects:
[NSArray arrayWithObjects: personName, phoneNumbers, nil]
forKeys:[NSArray arrayWithObjects: #"Name", #"Phones", nil]];
NSData *plistData = [NSPropertyListSerialization dataFromPropertyList:plistDict
format:NSPropertyListXMLFormat_v1_0
errorDescription:&errorDesc];
if (plistData)
{
[plistData writeToFile:bundlePath atomically:YES];
}
else {
NSLog(errorDesc);
[errorDesc release];
}
return NSTerminateNow;
}
You can find this information in the Property list programming guide
Mey,
I'm not sure that I understand your statement about having an empty plist. I assume that you mean that if you read back the plist file that you created, it is null when you print it out. Suggesting that you are writing out an empty file or not reading correct or ...
I further assume that your intent is to replace the existing plist contents by a new plist while keeping the same name.
And caveat emptor - I'm new to Objective C etc. Here is a way to do that which I think you are trying to do.
// Implement viewDidLoad to do additional setup after loading the view,
// typically from a nib.
- (void)viewDidLoad {
NSBundle *bundle = [NSBundle mainBundle];
NSString *plistPath = [bundle pathForResource:#"TmpPList" ofType:#"plist"]; //Not NARC
//NSLog(#"plistPath : %#", plistPath);
//My plist is a simple array, but it could be an array of dictionary objects etc
NSMutableArray *arrayFromPList = [[NSMutableArray alloc] initWithContentsOfFile:plistPath]; //NARC
//NSLog(#"arrayFromPList : %#", arrayFromPList);
//Delete the arrays contents and put new contents
[arrayFromPList removeAllObjects];
//NSLog(#"arrayFromPList : %#", arrayFromPList);
//[arrayFromPList addObjectsFromArray:[NSArray arrayWithObjects:#"A", #"B", "#C", nil]];
//NSLog(#"arrayFromPList : %#", arrayFromPList);
[arrayFromPList setArray:[NSMutableArray arrayWithObjects:#"A", #"B", #"C", #"D", #"E", #"F", nil]];
//NSLog(#"arrayFromPList : %#", arrayFromPList);
/* */
//Write it out to the original file name
[arrayFromPList writeToFile:plistPath atomically:YES];
NSMutableArray *newArray = [[NSMutableArray alloc] initWithContentsOfFile:plistPath]; //NARC
NSLog(#"newArray : %#", newArray);
[arrayFromPList release];
[newArray release];
}