iphone: problem in NSMutableArray and NSMutableDictionary - iphone

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];

Related

Complex Json Data and NSMutableDictionary parsing issue

Hi I'm using SBJson for moving Json data in and out of NSMutableDictionar, Im Building the main NSMutableDictionary from few others NSMutableDictionary like this
- (NSMutableDictionary *) getGeneral{
NSMutableDictionary *pType = [[NSMutableDictionary alloc]init];
[pType setObject:[NSNumber numberWithInteger:3] forKey:#"Ptype"];
NSMutableDictionary *session = [[NSMutableDictionary alloc]init];
[session setObject:[NSNumber numberWithInteger:-1] forKey:#"user_id"];
[session setObject:#"3" forKey:#"device_token"];
[session setObject:[NSNumber numberWithInteger:-1] forKey:#"customer_id"];
[session setObject:#"3" forKey:#"client_time"];
NSMutableDictionary *Error = [[NSMutableDictionary alloc]init];
[Error setObject:[NSNumber numberWithInteger:-1] forKey:#"error_code"];
[Error setObject:#"3" forKey:#"error_message"];
NSMutableDictionary *Successful = [[NSMutableDictionary alloc]init];
[Successful setObject:[NSNumber numberWithInteger:-1] forKey:#"success_code"];
[Successful setObject:#"3" forKey:#"success_message"];
NSMutableDictionary *Details = [[NSMutableDictionary alloc]init];
[Details setObject:#"3" forKey:#"user_name" ];
[Details setObject:#"3" forKey:#"user_password" ];
[Details setObject:[NSNumber numberWithInteger:-1] forKey:#"StartCallID"];
[Details setObject:#"3" forKey:#"StartDate" ];
[Details setObject:#"3" forKey:#"EndDate"];
NSMutableDictionary *General = [[NSMutableDictionary alloc]init];
[General setObject:pType forKey:#"Ptype"];
[General setObject:session forKey:#"Session"];
[General setObject:Error forKey:#"Error"];
[General setObject:Successful forKey:#"Successful"];
[General setObject:Details forKey:#"Details"];
return General;
}
and then i assign data into it i expected to get this Json structure:
{
"Ptype":[{"Ptype":-1}],
"Session":[{
"user_id":-1,
"device_token":" ",
"customer_id":-1,
"client_time":"",
}],
"Error":[{"error_code":-1,
"error_message":""}],
"Successful":[{"success_code":-1,
"success_message":""}],
"Details":[{
"user_name":" ",
"user_password":" ",
"StartCallID":-1,
"StartDate":" ",
"EndDate":" "
}]}
but there is no "]" or "[" in my json it looks like this, the order also change but this is not a problem, i take care of it on the server, issue is no square brackets
{"Session":
{"customer_id":-1,
"client_time":"3",
"user_id":-1,
"device_token":"3"},
"Error":{"error_code":-1,"error_message":"3"},
"Successful":{"success_code":-1,"success_message":"3"},
"Details":{"StartCallID":-1,
"user_password":"gg",
"user_name":"ff",
"StartDate":"3",
"EndDate":"3"},
"Ptype":{"Ptype":3}}
any one know on this issue, i need multiple items with the same name and this is the json standart for it
Thanks
Square brackets surrounds an array and you only have dictionaries.
The key is unique within each dictionary.
For example to put customer_id in a dictionary within an array:
NSArray *myArray = [NSArray arrayWithObjects:
[NSDictionary dictionaryWithObjectsAndKeys:
#"-1",
#"customer_id",
nil] nil];
Link of interest Understand JSON 3min lesson

Using a nested NSMutableDictionary

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"];

Number of Objects (3) not equal to number of keys(8)

Ok, so the issue is coming from trying to pull data from a sqlite DB and place it in an array for a scroll view display. Im using FM Database library to connect to sql database
The code is as follows:
NSMutableArray *data = [[NSMutableArray alloc] init];
FMResultSet *result = [[[StorageTank sharedStorageTank] DB]
executeQuery:#"SELECT * FROM table"];
while([result next])
{
NSArray *values = [[NSArray alloc] initWithObjects:
[[NSNumber alloc] initWithInt:[result intForColumn:#"id"]],
[[NSNumber alloc] initWithInt:[result intForColumn:#"count"]],
[[NSNumber alloc] initWithInt:[result intForColumn:#"required"]],
[result stringForColumn:#"image_portrait"],
[result stringForColumn:#"image_landscape"],
[[NSNumber alloc] initWithInt:[result intForColumn:#"end_date"]],
[[NSNumber alloc] initWithInt:[result intForColumn:#"active"]],
[result stringForColumn:#"merchant"], nil];
NSLog(#"%#", values);
NSArray *keys = [[NSArray alloc] initWithObjects: #"id",#"count",#"required",
#"image_portrait",#"image_landscape",
#"end_date",#"active",#"merchant",nil];
NSLog(#"%#", keys);
NSDictionary *row = [[NSDictionary alloc] initWithObjects: values forKeys: keys];
[data addObject: row];
}
NSArray *resultArray = [[NSArray alloc] init];
resultArray = data;
So, obviously from the code i've tested to make sure the values count is equal to the keys count... yet Im still getting this error:
"Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSPlaceholderDictionary initWithObjects:forKeys:]: number of objects (3) not equal to number of keys (8)'"
I can't understand for the life of me why the count would differ if when I print out the values array I see 8 values... which should match my 8 keys? and they are correct?
Any help/direction would be greatly appreciated!
Thanks,
Is the fourth item in your values array:
[result stringForColumn:#"image_portrait"]
returning nil? That's the value that tells -initWithObjects that the list is done.

how to edit an NSMutableArray data?

I have this NSMutableArray
sections = [[NSMutableArray alloc] init];
[sections addObject:[[NSMutableDictionary alloc] initWithObjectsAndKeys:#"Electricity",#"Type",#"0",#"Count", nil]];
[sections addObject:[[NSMutableDictionary alloc] initWithObjectsAndKeys:#"Water",#"Type",#"0",#"Count", nil]];
[sections addObject:[[NSMutableDictionary alloc] initWithObjectsAndKeys:#"Mobile",#"Type",#"0",#"Count", nil]];
[sections addObject:[[NSMutableDictionary alloc] initWithObjectsAndKeys:#"Internet",#"Type",#"0",#"Count", nil]];
[sections addObject:[[NSMutableDictionary alloc] initWithObjectsAndKeys:#"Fixed Line",#"Type",#"0",#"Count", nil]];
and lets say a user choose to change Water count from 0 to 10 how to do that?
[[sections objectAtIndex:1] setObject:#"10" forKey:#"Count"];
One more thing. When you create objects with alloc+init, you should release them yourself. So for each NSMutableDictionary you have a memory leak (in your code).
Use either [[[NSMutableDictionary alloc] initWithObjectsAndKeys:..., nil] autorelease]; or [NSMutableDictionary dictionaryWithObjectsAndKeys:..., nil]; (of course, you can make something like for (NSMutableDictionary *dict in sections) [dict release]; later, but this looks ugly).

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];