Create an NSDictionary of NSDictionaries - iphone

Believe it or not, I have searched the internet before asking this question. Unbelievably, I have yet to find a nice clear example of how to create an NSDictionary of NSDictionaries.
Here is my code so far, but it prints null. Any ideas?
// Here I am creating the dictionaries in the code until I start getting them from the server ;)
NSArray *keys = [NSArray arrayWithObjects:#"mission", #"target", #"distance",#"status", nil];
NSArray *objectsA = [NSArray arrayWithObjects:#"tiger", #"bill", #"5.4km", #"unknown", nil];
NSDictionary *tiger = [NSDictionary dictionaryWithObjects:objectsA
forKeys:keys];
NSArray *objectsB = [NSArray arrayWithObjects:#"bull", #"roger", #"10.1km", #"you are dead", nil];
NSDictionary *bull = [NSDictionary dictionaryWithObjects:objectsB
forKeys:keys];
NSArray *objectsC = [NSArray arrayWithObjects:#"peacock", #"geoff", #"1.4km", #"target liquidated", nil];
NSDictionary *peacock = [NSDictionary dictionaryWithObjects:objectsC
forKeys:keys];
// activeMissions = [NSArray arrayWithObjects:tiger, bull, peacock, nil];
[activeMissions setObject:tiger forKey:#"tiger"];
[activeMissions setObject:bull forKey:#"bull"];
[activeMissions setObject:peacock forKey:#"peacock"];
NSLog(#"active Missions %#", activeMissions);

You are not intializing activeMissions, that is why the NSLog statement is printing null (sending a message to a nil object in ObjC return nil).
Put this before assigning to activeMissions:
NSMutableDictionary *activeMissions = [NSMutableDictionary dictionaryWithCapacity:3];
Otherwise, if you prefer having a non mutable NSDictionary, you could do:
NSDictionary *activeMissions = [NSDictionary dictionaryWithObjects: [NSArray arrayWithObjects:tiger, bull, peacock, nil]
forKeys: [NSArray arrayWithObjects:#tiger, #"bull", #"peacock", nil]];
(Keep in mind that this is autoreleased, you'll have to retain somehow).

Related

NSArray losing values when I put self into it

So I have this piece of code:
- (void) connectSelector:(NSArray *)args {
NSError* error;
NSString* data = [NSString stringWithContentsOfURL:[NSURL URLWithString:[args objectAtIndex:0]] encoding:NSASCIIStringEncoding error:&error];
NSLog(#"%#", data);
NSDictionary* dictionary = [[NSDictionary alloc] initWithObjects:[NSArray arrayWithObjects:#"connector", #"data", #"error", nil] forKeys:[NSArray arrayWithObjects:self, data, error, nil]];
[[args objectAtIndex:1] performSelector:#selector(dataDownloaderDidDownloadData:) withObject:dictionary];
}
The values NSArray in dictionary is losing its 'self' value. Why is this so?
Thanks in advance!
Looks like you've got the keys and values element back-to-front. You probably meant:
NSDictionary* dictionary = [[NSDictionary alloc] initWithObjects:[NSArray arrayWithObjects:self, data, error, nil] forKeys:[NSArray arrayWithObjects:#"connector", #"data", #"error", nil] ];
Or to use modern syntax:
NSDictionary *dictionary = #{ #"connector" : self, #"data" : data, #"error" : error };
(Not every object can be used as a key in a dictionary).
Might be the case with arrayWithObjects(but not sure),because it returns an auto-released array.Use initWithObjects which returns an array you must then release to avoid a memory leak.
Check This : iPad large NSArray - initWithObjects vs. ArrayWithObjects

Retrive values from NSDictionary

I am trying to retrieve the values from my NSDictionary however I am running into errors, I was wondering if someone might be able to help me with my solution.
I have 18 values, not all are shown here that I am checking, when the correct key is reached I would like to take the value in the NSDictionary and pass it into one of my NSString variables.
Below is an example of what I am trying to do however like I say I am having several issues
for (id key in searchData) {
if ([[searchData objectForKey:key] isEqualToString:#"Code"]) {
codeSeries = [searchData objectForKey:key];
}
else if ([[searchData objectForKey:key] isEqualToString:#"ID"]) {
IDSeries = [searchData objectForKey:key];
}
// ...
However, when I try to log out any of the values, they all return Null. I have checked the dictionary before hand and the values are all most definitely in there, so I am thinking there is something wrong with my code above.
Any help would be greatly appreciated.
Update
This is how I create the NSDictionary
//start of mymethod...
NSDictionary *sendSeriesDictionary = [[NSDictionary alloc] init];
// Keys for sendSeriesDictionary
NSArray *keys = [NSArray arrayWithObjects:#"Code", #"ID", nil];
// Objects for keys that are for sendSeriesDictionary
NSArray *objects = [NSArray arrayWithObjects: [NSNull null], IdString, nil];
// Add keys and objects to NSDictionary
sendSeriesDictionary = [NSDictionary dictionaryWithObjects:objects forKeys:keys];
[engineRequests SeriesSearch:sendSeriesDictionary]; // this is where I send the NSDictionary over to where I want to read it.
//end of mymethod
You've got several problems. First, you mix up keys and values (like Tom said). Second, you have a possible memory leak where you create the dictionary, or at least a unnecessary instantiation.
Try this for creating the dictionary:
// Keys for sendSeriesDictionary
NSArray *keys = [NSArray arrayWithObjects:#"Code", #"ID", nil];
// Objects for keys that are for sendSeriesDictionary
NSArray *objects = [NSArray arrayWithObjects: [NSNull null], IdString, nil];
// Add keys and objects to NSDictionary
NSDictionary *sendSeriesDictionary = [NSDictionary dictionaryWithObjects:objects forKeys:keys];
Retrieving the values can be done like this:
codeSeries = [searchData objectForKey:#"Code"];
IDSeries = [searchData objectForKey:#"ID"];
In your first loop you looped through all the keys, got their values and then compared those to the key again. Which makes no sense.
Are you mixing keys and values ?
Maybe you just want codeSeries = [searchData objectForKey:#"Code"]; ?

AFNetworking - Build NSDictionary parameters

I'm trying to build my NSDictionnary to send to a request using the AFNetworking framework, but it seems that I'm quite confused about how to do it properly.
Here's what the server is expecting :
{
"limit":10,
"filters":
[
{"field":"owner","operator":"EQUAL","value":"ownerId","type":"integer"},
{"field":"date","operator":"GE","value":"30 Jun 2010 00:00:00","type":"date"},
],
"order":[{"field":"date","order":"ASC"}],
"page":0
}
What I'm trying to do (I don't really know if it's the right way to do it tbh), is to build a NSDictionary like the following :
NSDictionary *parameters = [NSDictionary dictionaryWithObjectsAndKeys:
#"10", #"chunkSize",
[NSDictionary dictionaryWithObjectsAndKeys:
[NSDictionary dictionaryWithObjectsAndKeys:#"owner", #"field", #"EQUAL", #"operator", #"ownerId", #"value", #"integer", #"type", nil],
[NSDictionary dictionaryWithObjectsAndKeys:#"date", #"field", #"GE", #"operator", #"30 Jun 2010 00:00:00", #"value", #"date", #"type", nil],
nil], #"filters",
[NSDictionary dictionaryWithObjectsAndKeys:
[NSDictionary dictionaryWithObjectsAndKeys:#"date", #"field", #"ASC", #"order", nil],
nil], #"order",
#"0", #"page",
nil];
But I have the following error when the view is loading :
*** Terminating app due to uncaught exception 'NSInvalidArgumentException',
reason: '+[NSDictionary dictionaryWithObjectsAndKeys:]: second object of each pair must be non-nil
I know I screw up building the parameters properly, but I can't manage to do it after several tries. Could anyone help ? Moreover, I don't really know the differences that I must implement here with the [] and the {}. I read that {} was for a dictionary and [] for an array, but I don't really see how to translate it in my case.
Your mistake is that the value for the dictionary starting on line 3 needs to be wrapped in an array.
At least until Objective-C array and hash literals go mainstream, my preferred method for creating complex dictionaries is to build them from an NSMutableDictionary. In your case:
NSMutableDictionary *mutableParameters = [NSMutableDictionary dictionary];
[mutableParameters setValue:#"10" forKey:#"limit"];
// ...
NSMutableArray *mutableFilters = [NSMutableArray array];
NSMutableDictionary *mutableOwnerFilterDictionary = [NSMutableDictionary dictionary];
[mutableOwnerFilterDictionary setValue:#"owner" forKey:#"field"];
// ...
[mutableFilters addObject:mutableOwnerFilterDictionary];
[mutableParameters setValue:mutableFilters forKey:#"filters"];
// ...
Also, be sure you're sending that over as JSON by setting AFJSONParameterEncoding to your AFHTTPClient.
The brackets [] signify an array, while the braces {} signify an object (dictionary in this context). To produce the structure you require:
NSDictionary *parameters = [NSDictionary dictionaryWithObjectsAndKeys:
#"10", #"chunkSize",
[NSArray arrayWithObjects:
[NSDictionary dictionaryWithObjectsAndKeys:#"owner", #"field", #"EQUAL", #"operator", #"ownerId", #"value", #"integer", #"type", nil],
[NSDictionary dictionaryWithObjectsAndKeys:#"date", #"field", #"GE", #"operator", #"30 Jun 2010 00:00:00", #"value", #"date", #"type", nil],
nil], #"filters",
[NSArray arrayWithObjects:
[NSDictionary dictionaryWithObjectsAndKeys:#"date", #"field", #"ASC", #"order", nil],
nil], #"order",
#"0", #"page",
nil];

printing values of keys in NSDictionary

What I am having so far right now is
NSArray *keys = [NSArray arrayWithObjects:#"firstName",#"lastName",#"phoneNumber",#"email",#"password",nil];
NSArray *objects = [NSArray arrayWithObjects:#"nil",#"nil",#"nil",#"nil",#"abc",nil];
dictionary = [NSDictionary dictionaryWithObject:objects forKey:keys];
NSLog(#"pass is %#",[keys objectAtIndex:4]);
NSLog(#"value of pass is%#",[dictionary objectForKey:#"password"]);
However, What I got from the debugger is
pass is password
value of pass is (null)
Can anyone explain why the value is null.It should be abc,shouldn't it.
The following line would have given a warning:
dictionary = [NSDictionary dictionaryWithObject:objects forKey:keys];
It should read:
NSDictionary *dictionary = [NSDictionary dictionaryWithObjects:objects forKeys:keys];
Change this line you are missing s
NSDictionary *dictionary = [NSDictionary dictionaryWithObjects:objects forKeys:keys];
you can do it in the fallowing way also...
NSMutableDictionary *dictionary=[NSMutableDictionary dictionaryWithObjectsAndKeys:#"nil",#"firstName",#"nil",#"lastName",#"nil",#"phoneNumber",#"nil",#"email",#"abc",#"password", nil];
NSLog(#"value of pass is%#",[dictionary objectForKey:#"password"]);

Create a dictionary property list programmatically

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. ;)
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:
The method +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 terminating nil argument.
Simplify the creation code. You don't need a million local variables just to create the thing. Use inline arguments where it makes sense. One way to do this it to build up your dictionary in code as an NSMutableDictionary, then (if necessary) make an immutable copy of it by calling -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.
Use +arrayWithObject: for creating an NSArray with a single object.
(Style suggestions) Never use an uppercase letter to begin the name of a variable, method, or function. (Notice that SO highlights leading-caps variables like class names.) It will certainly help others who read your code from being confused about your intent by your naming scheme.
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.)
NSDictionary *item1 = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:#"Screen J",[NSNumber numberWithInt:3],nil]
forKeys:[NSArray arrayWithObjects:#"Title",#"View",nil]];
NSDictionary *item2 = [NSDictionary dictionaryWithObjectsAndKeys:
#"Screen I", #"Title",
[NSArray arrayWithObject:item1], #"Children",
nil];
...
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 method dictionaryWithObjectsAndKeys: which does the same thing but keeps the keys with the objects:
NSDictionary *item1 = [NSDictionary dictionaryWithObjectsAndKeys:
#"Screen J",
#"Title",
[NSNumber numberWithInt:3],
#"View"];
NSMutableDictionary *topLevel = [NSMutableDictionary dictionary];
NSMutableDictionary *item1 = [NSMutableDictionary dictionary];
NSString *item1title = [NSString stringWithString:#"Title 1"];
NSMutableDictionary *item1children = [NSMutableDictionary dictionary];
// create children
NSString *item1child1 = [NSString stringWithString:#"item 1, child 1"];
NSMutableDictionary *item1child2 = [NSMutableDictionary dictionary];
NSString *item1child2title = [NSString stringWithString:#"Title 1-2"];
NSMutableDictionary *item1child2children = [NSMutableDictionary dictionary];
NSString *item1child2child1 = [NSString stringWithString:#"item 1, child 2, child 1"];
NSString *item1child2child2 = [NSString stringWithString:#"item 1, child 2, child 2"];
[item1child2 setObject:item1child2title forKey:#"Title"];
[item1child2children setObject:item1child2child1 forKey:#"item 1 child2 child 1"];
[item1child2children setObject:item1child2child2 forKey:#"item 1 child2 child 2"];
[item1child2 setObject:item1child2children forKey:#"children"];
// add children to dictionary
[item1children setObject:item1child1 forKey:#"item1 child1"];
[item1children setObject:item1child2 forKey:#"item1 child2"];
// add to item 1 dict
[item1 setObject:item1title forKey:#"Title"];
[item1 setObject:item1children forKey:#"children"];
NSMutableDictionary *item2 = [NSMutableDictionary dictionary];
NSString *item2title = [NSString stringWithString:#"Title"];
NSMutableDictionary *item2children = [NSMutableDictionary dictionary];
NSString *item2child1 = [NSString stringWithString:#"item 2, child 1"];
NSString *item2child2 = [NSString stringWithString:#"item 2, child 2"];
NSString *item2child3 = [NSString stringWithString:#"item 2, child 3"];
// add children to dictionary
[item2children setObject:item2child1 forKey:#"item2 child1"];
[item2children setObject:item2child2 forKey:#"item2 child2"];
[item2children setObject:item2child3 forKey:#"item2 child3"];
// add to item 2 dict
[item2 setObject:item2title forKey:#"Title"];
[item2 setObject:item2children forKey:#"children"];
[topLevel setObject:item1 forKey:#"Item 1"];
[topLevel setObject:item2 forKey:#"Item 2"];
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
NSDictionary *root = [NSMutableDictionary dictionary];
NSDictionary *item1 = [NSDictionary dictionaryWithObject:[NSArray arrayWithObject:#"VirtuaGym Online"] forKey:[NSArray arrayWithObjects:#"Title"]];
NSDictionary *item2 = [NSDictionary dictionaryWithObject:[NSArray arrayWithObject:#"Do the training"] forKey:[NSArray arrayWithObjects:#"Title"]];
NSDictionary *item3 = ...
[root setObject:item1 forKey:#"Item 1"];
[root setObject:item2 forKey:#"Item 2"];
Also did some research and tried something else with some other input..
NSMutableArray *Rows = [NSMutableArray arrayWithCapacity: 1];
for (int i = 0; i < 4; ++i) {
NSMutableArray *theChildren = [NSMutableArray arrayWithCapacity: 1];
[theChildren addObject: [NSString stringWithFormat: #"tester %d", i]];
NSString *aTitle = [NSString stringWithFormat: #"Item %d", i];
NSDictionary *anItem = [NSDictionary dictionaryWithObjectsAndKeys: aTitle, #"Title", theChildren, #"Children"];
[Rows addObject: anItem];
}
NSDictionary *Root = [NSDictionary withObject: Rows andKey: #"Rows"];
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
NSDictionary *item1 = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:#"Screen J",[NSNumber numberWithInt:3],nil]
forKeys:[NSArray arrayWithObjects:#"Title",#"View",nil]];
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 ;)