I want to programatically create a dictionary which feeds data to my UITableView but I'm having a hard time with it. I want to create a dictionary that resembles this property list (image) give or take a couple of items.
I've looked at "Property List Programming Guide: Creating Property Lists Programmatically" and I came up with a small sample of my own:
//keys
NSArray *Childs = [NSArray arrayWithObjects:@"testerbet", nil];
NSArray *Children = [NSArray arrayWithObjects:@"Children", nil];
NSArray *Keys = [NSArray arrayWithObjects:@"Rows", nil];
NSArray *Title = [NSArray arrayWithObjects:@"Title", nil];
//strings
NSString *Titles = @"mmm training";
//dictionary
NSDictionary *item1 = [NSDictionary dictionaryWithObject:Childs, Titles forKey:Children , Title];
NSDictionary *item2 = [NSDictionary dictionaryWithObject:Childs, Titles forKey:Children , Title];
NSDictionary *item3 = [NSDictionary dictionaryWithObject:Childs, Titles forKey:Children , Title];
NSArray *Rows = [NSArray arrayWithObjects: item1, item2, item3, nil];
NSDictionary *Root = [NSDictionary dictionaryWithObject:Rows forKey:Keys];
// NSDictionary *tempDict = [[NSDictionary alloc] //initWithContentsOfFile:DataPath];
NSDictionary *tempDict = [[NSDictionary alloc] initWithDictionary: Root];
I'm trying to use this data of hierachy for my table views.
So I was wondering how can I can create my property list (dictionary) programmatically so that I can fill it with my own arrays.
I'm still new with iPhone development so bear with me. ;)
I am not sure I understand the basic objective here.
It seems like at runtime, you are constructing a deep dictionary with many child nodes. But you are constructing this all with static code... why can you not simply make a plist (like the one you had an image of) and read that into an NSDictionary? Both NSDictionary and NSArray have methods that let you simply read in a file and get a whole filled out object. Then it is WAY easier to edit and to understand. That method is
dictionaryWithContentsOfFile
.If all of the data is truly created at runtime before it is put into the dictionary, then it seems like you would want a very different, recursive, style of code rather than the flat examples given.
Lastly, I personally dislike the
dictionaryWithObjects:forKeys:
method in NSDictionary for building a dictionary. If you have to do things that way I greatly prefer the alternate methoddictionaryWithObjectsAndKeys:
which does the same thing but keeps the keys with the objects:first of .. super! thanks .. I really appreciate the explanation and code snippet. Since you gave me such a good explanation I hope you don't mind me asking a couple more questions.
First, I did as you suggested and this is what I came up with : (I used my original property list instead of the example this time so this is where the drilldown table gets his( or needs to get his ) treestructure).
http://img509.imageshack.us/img509/7523/picture2lsg.png
Also did some research and tried something else with some other input..
I decided to just test both of these however it does what I want. It gives me a
EXC_BAD_ACCESS
error.I was also wondering since I saw you using number in your code snippet, couldn't you also use NSString since that's what the plist uses.. could be totally of here of course
and third a question is about my possible approach to my app. I have an xml parser which saves certain information in different arrays. I want to use this information for my drilldown UITableviews (infoFirstScreen[] infoSecondScreen[] infoThirdScreen[]). The information provided has to be connected like the tree I showed you above. This is the reason I wanted to build the dictionary in code so I can take the info from my arrays and insert it here. My question do you think my approach is correct, wrong or is there a faster way?
again really appreciate the explanation ;)
This is a situation where "teach a man to fish" is a vastly more helpful approach than "give a man a fish". Once you understand the basic principles and the NSDictionary API, it becomes much easier to craft your own custom solution. Here are a few observations and learning points:
+dictionaryWithObject:forKey:
is used to create an NSDictionary with a single key-value pair. It will not accept comma-separated arguments after each colon (:) in the method call, just one. To create a dictionary with multiple key-value pairs, use one of 2 related methods:+dictionaryWithObjects:forKeys:
which accepts two NSArray objects containing values and keys, or+dictionaryWithObjectsAndKeys:
which alternates (object, key, object, key) with a terminatingnil
argument.-copy
on it. (Remember that a copy method returns a new object that you must release to avoid memory leaks.) That way you don't have to have a variable for every single value so you can do a "one shot" creation of the structure(s) at each level.+arrayWithObject:
for creating an NSArray with a single object.Just to give you a flavor of what creating the dictionary in the linked image might look like in code... (I'm ignoring your chosen variable names and using both different approaches for completeness.)