Reputation: 5510
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.
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
Upvotes: 0
Views: 282
Reputation: 15218
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.
Upvotes: 1
Reputation: 1349
Are you mixing keys and values ?
Maybe you just want codeSeries = [searchData objectForKey:@"Code"];
?
Upvotes: 1