Using a nested NSMutableDictionary - iphone

simple question, I need to structure data in the following format:
UserID:
1 {
Name = Bob
Surname = Hope
}
2 {
...
I can used an NSMutableDictionary to add a single layer with keys, but I am unable to create a child associate with a certain key.
I have tried creating a mutable dictionary and assigning that to a key:
NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];
NSArray *keys = [[NSArray alloc] initWithObjects:#"Name", #"Surname", nil];
NSArray *details = [[NSArray alloc] initWithObjects:#"Bob",#"Hope", nil];
NSDictionary *person = [[NSMutableDictionary alloc] initWithObjects:details forKeys:keys];
[dic setValue:#"1" forKey:#"id"];
[[dic objectForKey:#"id"] setDictionary: person];

I think it is better to use an array of dictionaries, each dictionary representing a user.
NSMutableArray *users= [[NSMutableArray alloc] init];
NSArray *keys = [[NSArray alloc] initWithObjects:#"Name", #"Surname", nil];
NSArray *details = [[NSArray alloc] initWithObjects:#"Bob",#"Hope", nil];
NSDictionary *person = [[NSMutableDictionary alloc] initWithObjects:details forKeys:keys];
[users addObject:person];

u can also set the objects for dictionary in this way
[dic setObject:person forKey:#"id"];

Related

creating JSON format in Objective C

For an application i want to create a JSON in format as mentioned below,
"Students" : {
"results": {
"Grade1": {
"studentresult": "pass",
"marksheet": "provided"
},
"ID": 01,
"Name": "Student1",
}
}
I am using the following code to create the same,
NSMutableDictionary *gradedetails = [[NSMutableDictionary alloc] init];
[gradedetails setObject:#"pass" forKey:#"studentresult"];
[gradedetails setObject:#"provided" forKey:#"marksheet"];
NSMutableDictionary *sdetails = [[NSMutableDictionary alloc] init];
[sdetails setObject:#"01" forKey:#"ID"];
[sdetails setObject:#"Name" forKey:#"Student1"];
NSMutableDictionary *grade = [[NSMutableDictionary alloc] init];
[grade setObject:gradedetails forKey:#"Grade1"];
NSMutableArray *rarray = [[NSMutableArray alloc] init];
[rarray addObject:grade];
[rarray addObject:sdetails];
NSMutableDictionary *results = [[NSMutableDictionary alloc] init];
[results setObject:rarray forKey:#"results"];
NSMutableDictionary *stud = [[NSMutableDictionary alloc] init];
[stud setObject:rdic forKey:#"Students"];
NSData *jsondata = [NSJSONSerialization dataWithJSONObject:stud options:NSJSONWritingPrettyPrinted error:&error];
I am getting in the following format,
"Students" : {
"results" : [
{
"Grade1" : {
"studentresult" : "pass",
"marksheet" : "provided"
}
},
{
"ID" : "01",
"Name" : "Student1"
}
]
}
}
could someone please help me in creating the format.
Thanks.
Required Data , You can get this way . Just convert that stud dict in JSon or any other format you want. Remove that array , You don't need it , As you mentioned it in the required format.
NSMutableDictionary *gradedetails = [[NSMutableDictionary alloc] init];
[gradedetails setObject:#"pass" forKey:#"studentresult"];
[gradedetails setObject:#"provided" forKey:#"marksheet"];
NSMutableDictionary *sdetails = [[NSMutableDictionary alloc] init];
[sdetails setObject:#"01" forKey:#"ID"];
[sdetails setObject:#"Name" forKey:#"Student1"];
[sdetails setObject:gradedetails forKey:#"Grade1"];
NSMutableDictionary *results = [[NSMutableDictionary alloc] init];
[results setObject:sdetails forKey:#"results"];
NSMutableDictionary *stud = [[NSMutableDictionary alloc] init];
[stud setObject:results forKey:#"Students"];
NSLog(#"Required Format Data is %#",stud);
Depends on your code:
NSMutableDictionary *gradedetails = [[NSMutableDictionary alloc] init];
[gradedetails setObject:#"pass" forKey:#"studentresult"];
[gradedetails setObject:#"provided" forKey:#"marksheet"];
NSMutableDictionary *results = [[NSMutableDictionary alloc] init];
[results setObject:gradedetails forKey:#"Grade1"];
[results setObject:#"01" forKey:#"ID"];
[results setObject:#"Name" forKey:#"Student1"];
NSMutableDictionary *stud = [[NSMutableDictionary alloc] init];
[stud setObject:results forKey:#"Students"];
NSDictionary *gradedetails = #{#"studentresult" : #"pass", #"marksheet" : #"provided"};
NSDictionary *grade = #{ #"Grade1" : gradedetails}
NSDictionary *sdetails = #{#"ID" : #"01", #"Student1" : #"Name"};
NSArray *resultsArray = #[grade, sdetails];
NSDictionary *results= #{#"results" : resultsArray};
NSDictionary *stud = #{#"Students" : results};
NSData *jsondata = [NSJSONSerialization dataWithJSONObject:stud options:NSJSONWritingPrettyPrinted error:&error];
I wonder why few developper use this notation
check this code-
NSMutableDictionary *students=[NSJSONSerialization JSONObjectWithData:webData options:0 error:nil];
for (NSDictionary *dictionofstudents in students)
{
NSMutableDictionary *results=[dictionofstudents objectForKey:#"results"];
for (NSDictionary *dictionofresults in results)
{
NSMutableDictionary *grade1=[dictionofresults objectForKey:#"Grade1"];
for (NSDictionary *dictionofgrade1 in grade1)
{
NSString *studentresult=[dictionofgrade1 objectForKey:#"studentresult"];
NSString *marksheet=[dictionofgrade1 objectForKey:#"marksheet"];
[arrayofstudentresult addObject:studentresult];
[arrayofmarksheet addObject:marksheet];
}
NSString *ID=[dictionofresults objectForKey:#"ID"];
NSString *name=[dictionofresults objectForKey:#"Name"];
[arrayofID addObject:ID];
[arrayofname addObject:name];
}
}
In order to get the format you want out of it, you have to pack information in that particular format.
Your problem is right about here:
NSMutableArray *rarray = [[NSMutableArray alloc] init];
[rarray addObject:grade];
[rarray addObject:sdetails];
The desired format has no arrays, so why are you creating an array?
A hint for you:
You should be creating exactly 4 dictionaries, and no arrays.
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
NSMutableDictionary *result = [[NSMutableDictionary alloc] init];
NSMutableDictionary *grade = [[NSMutableDictionary alloc] init];
[grade setValue:#"pass" forKey:#"studentresult"];
[grade setValue:#"provided" forKey:#"marksheet"];
[result setValue:grade forKey:#"Grade1"];
[result setValue:#"01" forKey:#"ID"];
[result setValue:#"Student1" forKey:#"Name"];
[dict setValue:result forKey:#"results"];
NSMutableDictionary *stdnt = [[NSMutableDictionary alloc] init];
[stdnt setValue:dict forKey:#"Students"];
NSError *error = nil;
NSData *jsondata = [NSJSONSerialization dataWithJSONObject:stdnt options:NSJSONWritingPrettyPrinted error:&error];
NSString *s = [[NSString alloc] initWithData:jsondata encoding:NSUTF8StringEncoding];
NSLog(#"%#",s);
It may helps you.

-[__NSCFString objectForKey:]: unrecognized selector sent to instance

// Configure the cell.
cell.textLabel.text = [[self.drinks objectAtIndex:indexPath.row] objectForKey:#"Name"];
return cell;
The above code is generating this exception. What could be causing this and how can I fix it?
My Understand from Your question
You have array of drinks like
Drink *d1 = [[Drink alloc] init]
d1.name = #"Drink1";
d1.price = [NSNumber numberWithFloat:25.0];
Drink *d2 = [[Drink alloc] init]
d2.name = #"Drink2";
d2.price = [NSNumber numberWithFloat:35.0];
OR
NSArray *values1 = [NSArray arrayWithObjects:#"Drink1",[NSNumber numberWithFloat:25.0], nil];
NSArray *keys1 = [NSArray arrayWithObjects:#"Name",#"price", nil];
NSDictionary *d1 = [[NSDictionary alloc] initWithObjects:values1 forKeys:keys1];
NSArray *values2 = [NSArray arrayWithObjects:#"Drink2",[NSNumber numberWithFloat:55.0], nil];
NSArray *keys2 = [NSArray arrayWithObjects:#"Name",#"price", nil];
NSDictionary *d2 = [[NSDictionary alloc] initWithObjects:values1 forKeys:keys1];
self.drinks = [NSArray arrayWithObjects:d1,d2, nil];
[d1 release];
[d2 release];
From the above case
cell.textLabel.text = [[self.drinks objectAtIndex:indexPath.row] objectForKey:#"Name"];
the above statement is correct
You are getting exception means, You are inserting the objects into drinks array, that supports Key Value Pairs data and that is String Data.
I hope you are having like this
self.drinks = [NSArray arrayWithObjects:#"Drink1",#"Drink2", nil];
In this above case you have to go with
cell.textLabel.text = [self.drinks objectAtIndex:indexPath.row];
first try to determine what object is self.drinks,
then you can go finer depending if is an array with dictionaries?
do a log
[self.drinks objectAtIndex:0]
what do you see?

iphone: problem in NSMutableArray and NSMutableDictionary

Dont know whats wrong with this array
NSMutableArray *locations=[[NSMutableArray alloc] init];
NSMutableDictionary *aLocation=[[NSMutableDictionary alloc] init];
[aLocation setObject:#"26.8465108" forKey:#"lat"];
[aLocation setObject:#"80.9466832" forKey:#"long"];
[locations addObject:aLocation];
[aLocation removeAllObjects]
[aLocation setObject:#"26.846127990018164" forKey:#"lat"];
[aLocation setObject:#"80.97541809082031" forKey:#"long"];
[locations addObject:aLocation];
[aLocation removeAllObjects];
but every time I remove all objects from aLocations dictionary those values get deleted from locations array as well.
Please help me in this
This behaviour is correct and you can't reuse NSMutableDictionary as you do: NSMutableArray does not copy objects it contains - it just stores pointers to those objects. So if an object you added to array changes then array has pointer to that changed object.
To fix your code create NSMutableDictionary instance each time you need to add it to array (or create immutable dictionary if you actually don't need mutable object):
NSMutableArray *locations=[[NSMutableArray alloc] init];
NSMutableDictionary *aLocation= [NSMutableDictionary dictionaryWithObjectsAndKeys:
#"26.8465108", #"lat",#"80.9466832" ,#"long", nil ];
[locations addObject:aLocation];
aLocation= [NSMutableDictionary dictionaryWithObjectsAndKeys:
#"26.846127990018164", #"lat",#"80.97541809082031" ,#"long", nil ];
[locations addObject:aLocation];
NSMutableArray *locations=[[NSMutableArray alloc] init];
NSMutableDictionary *aLocation=[[NSMutableDictionary alloc] init];
[aLocation setObject:#"26.8465108" forKey:#"lat"];
[aLocation setObject:#"80.9466832" forKey:#"long"];
[locations addObject:aLocation];
[aLocation release];
NSMutableDictionary *aLocation1=[[NSMutableDictionary alloc] init];
[aLocation1 setObject:#"26.846127990018164" forKey:#"lat"];
[aLocation1 setObject:#"80.97541809082031" forKey:#"long"];
[locations addObject:aLocation1];
[aLocation1 release];
NSMutableArray *locations=[[NSMutableArray alloc] init];
NSMutableDictionary *aLocation= [NSMutableDictionary initWithObjectsAndKeys:
#"26.8465108", #"lat",#"80.9466832" ,#"long", nil ];
[locations addObject:aLocation];
aLocation= [NSMutableDictionary initWithObjectsAndKeys:
#"26.846127990018164", #"lat",#"80.97541809082031" ,#"long", nil ];
[locations addObject:aLocation];

Values not appending to my NSMutableDictionary

I'm trying to add some vales to a NSMutableDictionary dynamically. However, using the following code, I'm adding values using the first letter as the key to a temporary dictionary and then finally adding it to my names dictionary but it just overwrites each value for it's corresponding key
NSMutableDictionary *dictionary = [[[NSMutableDictionary alloc] init] autorelease];
for (NSString *drugStr in listContents) {
NSString *substring = [drugStr substringToIndex:1];
[dictionary setValue:drugStr forKey:substring];
}
names = [[NSDictionary alloc] initWithDictionary:dictionary];
[dictionary release];
What am I doing wrong?
You should define your NSDictionary to use NSString as a key, and NSArray as a value.
Then you should just retrieve your value according to the given key. If the result is nil, then you need to create a new NSMutableArray, to which you add the value above. IF the result is not-nil, add the value to the array.
NSMutableDictionary *dictionary = [[[NSMutableDictionary alloc] init] autorelease];
for (NSString *drugStr in listContents) {
NSString *substring = [drugStr substringToIndex:1];
NSMutableArray *valueArray = (NSMutableArray*)[dictionary objectForKey:substring];
if(valueArray==nil){
NSMutableArray *newArray = [NSMutableArray arrayWithCapacity:5];
[newArray addObject:drugStr];
[dictionary setObject:newArray forKey:substring];
}else{
[valueArray addObject:drugStr];
}
}
names = [[NSDictionary alloc] initWithDictionary:dictionary];
[dictionary release];

iPhone: how to NSArray content to NSDictionary

I want to add content of NSArray's to NSDictionary r NSMultDictionary.
I have NSDictionary(empty), and I have NSArray with content and I have to store NSArray content in NSDictionary.
How can I do this?
You can add content of NSArray to NSDictionary by using following code.
NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];
for(int i=0; i<[Array count]; i++){
[dic setObject:[Array objectAtIndex:i] forKey:[NSString stringWithFormat:#"%d",i]];
}
The dic is the dictionary, you can use that.
Or else you can add whole array as one object in the dictionary as
NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];
[dic setObject:Array forKey:#"array"];
#Nandakishore suppose you have an array eg:-myArray
NSDictionary *A = [NSDictionary dictionaryWithObject:myArray forKey:#"Key"];
Now this dictionary object(A) has all the content of the myArray......now do what u wanna do with your dictionary
I hope this may help you!