How to design Object Graph/Model? - iphone

JSON As Dictionary
{
headers = (
{
backGroundImageUrl = "";
dataField = Name;
headerText = Name;
id = Name;
itemRenderer = "";
toolTip = "";
width = 60;
},
{
backGroundImageUrl = "";
dataField = BidPrice;
headerText = Bid;
id = BidPrice;
itemRenderer = "";
toolTip = "Bid Price";
width = 30;
});
values = (
{
assetCellValueLst = {
AskColorCode = "#B8D1ED";
AskPrice = "102.20";
BidColorCode = "#B8D1ED";
BidPrice = "102.00";
Name = "AR Bonar 11";
PECSAsk = 569;
PECSChg = "(31)";
PECSChgColorCode = "#000000";
PriceChg = "0.00";
PriceChgColorCode = "#000000";
SOLAsk = 604;
SSPAsk = 677;
SSPChgDay = "+3";
SSPChgDayColorCode = "#000000";
YTMAsk = "6.97";
assetGroupName = Argentina;
assetId = ARBONAR11;
iconPath = "images/flag_Argentina.gif";
updated = false;
};
assetId = ARBONAR11;
},
{
assetCellValueLst = {
AskColorCode = "#53840f";
AskPrice = "84.00";
BidColorCode = "#53840f";
BidPrice = "83.75";
Name = "AR Bod 15";
PECSAsk = 945;
PECSChg = 14;
PECSChgColorCode = "#000000";
PriceChg = "-0.10";
PriceChgColorCode = "#53840F";
SOLAsk = 985;
SSPAsk = 1007;
SSPChgDay = "+7";
SSPChgDayColorCode = "#000000";
YTMAsk = "11.74";
assetGroupName = Argentina;
assetId = ARBON15;
iconPath = "images/flag_Argentina.gif";
updated = false;
};
assetId = ARBON15;
});
The above JSON has more headers and values shortened here for clarity. The assestCellValueLst Dictionary can have more or less key-value pair depending on business logic. Same is true for headers dictionary. How should I create a Object Model in Xcode to use it with Core Data Managed Objects ? for a non - persistent store or no store at all

Simplistically, you would just create an entity for each type of dictionary you wanted to persist and have an attribute for each key in the dictionary.
In this case it looks like you would have two entities: Header and Asset. If the headers have a one-to-relationship with assets you might have (pseudocode) entities like this:
Header{
backGroundImageUrl = string;
dataField = string;
headerText = string;
id = string;
itemRenderer = ?;
toolTip = ?
width = integer;
asset<--(required,cascade)-->Asset.header
}
Asset{
assetId = string;
askColorCode = string;
askPrice = float;
bidColorCode = string;
bidPrice = float;
//...etc
header<--(required,nullify)-->Header.asset
}
The basic idea is that the model, well, "models" whatever real world object, event, condition or information you want to persist. In this case, the JSON is providing nice, neat dictionaries with all the data already organized and labeled for you.

+(NSArray *)managedObjectsFromJSONDictionary:(NSDictionary *)dictionary withManagedObjectContext:(NSManagedObjectContext *)moc
{
NSMutableArray *finalArrayOfArrays = [[NSMutableArray alloc] init];
NSArray *allKeys = [dictionary allKeys];
for(NSString *keyName in allKeys)
{
id structureArray = [dictionary valueForKey:keyName];
if([structureArray isKindOfClass:[NSArray class]])
{
NSMutableArray *objectArray = [[NSMutableArray alloc] init];
for(NSDictionary *structureDictionary in (NSArray *)structureArray)
{
[objectArray addObject:[JSONtoManagedObject managedObjectFromDictionary:structureDictionary withManagedObjectContext:moc forEntity:keyName]];
}
[finalArrayOfArrays addObject:objectArray];
[objectArray release];
}
}
return [finalArrayOfArrays autorelease];
}
+(NSManagedObject *)managedObjectFromDictionary:(NSDictionary *)dictionary withManagedObjectContext:(NSManagedObjectContext *)moc forEntity:(NSString *)entityName
{
NSManagedObject *managedObject;
NSArray *arrayOfKeys = [dictionary allKeys];
for(NSString *name in arrayOfKeys)
{
id obj = [dictionary objectForKey:name];
managedObject = [NSEntityDescription insertNewObjectForEntityForName:entityName inManagedObjectContext:moc];
if (![obj isKindOfClass:[NSDictionary class]])
{
if([name isEqualToString:#"id"])
[managedObject setValue:obj forKey:#"uniqueID"];
else
[managedObject setValue:obj forKey:name];
}
else
{
NSDictionary *relationshipDictionary = [[managedObject entity] relationshipsByName];
for(NSString *relationshipName in [relationshipDictionary allKeys])
{
NSRelationshipDescription *relationshipDescription = [relationshipDictionary objectForKey:relationshipName];
if(![relationshipDescription isToMany] && [relationshipName isEqualToString:name])
{
NSManagedObject *childObject = [JSONtoManagedObject managedObjectFromDictionary:obj withManagedObjectContext:moc forEntity:relationshipName];
[managedObject setValue:childObject forKey:relationshipName];
continue;
}
NSMutableSet *relationshipSet = [managedObject mutableSetValueForKey:relationshipName];
NSArray *relationshipArray = [obj valueForKey:relationshipName];
for(NSDictionary *insideDictoinary in relationshipArray)
{
NSManagedObject *childObject = [JSONtoManagedObject managedObjectFromDictionary:insideDictoinary withManagedObjectContext:moc forEntity:relationshipName];
[relationshipSet addObject:childObject];
}
}
}//end else
} // for
return managedObject;
}

Related

Sorting array of multiple dictionaries

My array contains three dictionaries per object of the array.
{
avg = {
avg1 = 50;
avg2 = 60;
};
posts = {
alcoholContent = 450;
name = "BBB";
origin = United States;
};
reviews = {
rev1 = "Test review 1";
rev2 = "Test review 2";
};
}
{
avg = {
avg1 = 30;
avg2 = 20;
};
posts = {
alcoholContent = 550;
name = "AAA";
origin = United States;
};
reviews = {
rev1 = "Test review 1";
rev2 = "Test review 2";
};
}
I want to sort array acceding by key "name" (of post dictionary).
How can I do it?
I tried normal sorting methods using sort descriptors, but did not work
Try sortUsingComparator:
[array sortUsingComparator:^NSComparisonResult(id obj1, id obj2) {
NSDictionary *dict1 = obj1;
NSDictionary *dict2 = obj2;
NSString *string1 = [[dict1 objectForKey:#"posts"] objectForKey:#"name"];
NSString *string2 = [[dict2 objectForKey:#"posts"] objectForKey:#"name"];
return [string1 compare:string2];
}];

ordering of NSArray

I am trying to set up my TableView, and I am having trouble. The values of my NSArray is different from each other. I know this is hard to understand, but i will try my best to explain.
For example
-(void)setUpContacts{
//set up the contacts
NSString *string = #"ABCDEFGHIJKLMNOPQRSTUVWXYZ";
letterArray = [[NSMutableDictionary alloc]init];
Contacts *contact = [[Contacts alloc]init];
contactNumbers = [contact phoneNumbers];
for (NSDictionary* info in contactNumbers) {
firstLetter = [info objectForKey:#"lastName"];
index = #"_";
if([firstLetter length] > 0){
firstLetter =[firstLetter substringToIndex:1];
firstLetter= [firstLetter capitalizedString];
NSRange range = [string rangeOfString:firstLetter];
if(range.length >= 0){
index= firstLetter;
}
}
if(![letterArray objectForKey:index]){
[letterArray setValue:[NSMutableArray array] forKey:index];
}
[[letterArray objectForKey:index] addObject:info];
}NSLog(#"%#",letterArray);}
-(NSArray *)getLetterArraycount{
NSMutableArray *countArray = [[NSMutableArray alloc]init];
NSNumber *subCount;
for(id key_main in letterArray){
subCount = 0;
for(id key_sub in [letterArray objectForKey:key_main]){
subCount = [NSNumber numberWithInt:[subCount intValue]+1];
}
[countArray addObject:subCount];
}
return countArray;
}
- (void)viewDidLoad{
[super viewDidLoad];
[self setUpContacts];
countArrays = [self getLetterArraycount];
indexPaths = [[NSMutableDictionary alloc]init];
NSArray *temp = [letterArray allKeys];
NSLog(#"%#",temp);
NSLog(#"%#",countArrays);
//set up the tableView
self.myTableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
self.myTableView.delegate = self;
self.myTableView.dataSource = self;
self.title = #"Select Friends";
self.myTableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[self.view addSubview:myTableView];}
and it logs like this in the Console
C = (
{
firstName = Alex;
kind = Mobile;
lastName = Chang;
name = "Alex Chang (Mobile)";
number = "(555) 555-5555";
},
{
firstName = YuYu;
kind = Mobile;
lastName = Chen;
name = "YuYu Chen (Mobile)";
number = "(408) 112-2334";
},
{
firstName = Chris;
kind = Mobile;
lastName = Choi;
name = "Chris Choi (Mobile)";
number = "(999) 999-9999";
},
{
firstName = Kevin;
kind = Mobile;
lastName = Chung;
name = "Kevin Chung (Mobile)";
number = "1 (231) 241-2312";
}
);
H = (
{
firstName = Danny;
kind = Mobile;
lastName = Huang;
name = "Danny Huang (Mobile)";
number = "(408) 599-9770";
},
{
firstName = Ice;
kind = Mobile;
lastName = Huang;
name = "Ice Huang (Mobile)";
number = "(408) 444-4444";
}
);
K = (
{
firstName = Will;
kind = Mobile;
lastName = King;
name = "Will King (Mobile)";
number = "(415) 123-4567";
}
);
L = (
{
firstName = "";
kind = iPhone;
lastName = LastName;
name = " LastName (iPhone)";
number = "(408) 123-2555";
},
{
firstName = david;
kind = Mobile;
lastName = lub;
name = "david lub (Mobile)";
number = "(666) 666-1111";
}
);
P = (
{
firstName = Jennifer;
kind = Mobile;
lastName = Pie;
name = "Jennifer Pie (Mobile)";
number = "(666) 666-6666";
}
);
S = (
{
firstName = Martin;
kind = Mobile;
lastName = Shen;
name = "Martin Shen (Mobile)";
number = "(415) 333-1234";
},
{
firstName = Francis;
kind = Mobile;
lastName = Shung;
name = "Francis Shung (Mobile)";
number = "(408) 345-6789";
}
);
V = (
{
firstName = Brian;
kind = Mobile;
lastName = Vu;
name = "Brian Vu (Mobile)";
number = "(408) 234-5678";
}
);
Y = (
{
firstName = Qiu;
kind = Mobile;
lastName = Yang;
name = "Qiu Yang (Mobile)";
number = "(408) 567-8901";
}
);
Z = (
{
firstName = Joy;
kind = Mobile;
lastName = Zhou;
name = "Joy Zhou (Mobile)";
number = "(408) 765-4321";
}
);
"_" = (
{
firstName = FirstName;
kind = Mobile;
lastName = "";
name = "FirstName (Mobile)";
number = "1 (408) 234-5141";
}
);
}
2012-04-26 16:14:05.195 UpOut[2693:15803] (
L,
Y,
V,
"_",
Z,
S,
K,
C,
P,
H
MY Question is why doesnt it log "C" first, but instead it Logged "L"
Sounds like you need to learn how to use "NSSortDescriptor" (I've linked the documentation for you).
Here is a related question that might have some useful sample code you can refer to. The key you would want to sort on, in your case, would be the lastName value of each NSDictionary entry.
Alphabetical answers consistently get more upvotes:
NSArray *sortedArray = [countArray sortedArrayUsingSelector:#selector(localizedCaseInsensitiveCompare:)];

convert dictionary to array after removing key values

i need to remove first three keys values from each index of array. What i did is
for (int i=0; i<[normalscoringarray count]; i++)
{
NSDictionary *dictionary = [normalscoringarray objectAtIndex:i];
NSMutableDictionary *mutableDictionary = [dictionary mutableCopy];
[mutableDictionary removeObjectForKey:#"cn"];
[mutableDictionary removeObjectForKey:#"gdate"];
[mutableDictionary removeObjectForKey:#"gid"];
NSLog(#"mutableDictionary.....%#",mutableDictionary);
NSMutableArray *remainingArray = [[NSMutableArray alloc]init];
for (id item in mutableDictionary) {
[remainingArray setArray:[mutableDictionary objectForKey:item]];
}
}
and it does not give remainingArray in that order i want, more over it removes all keys.
normalscoringarray....(
{
cn = "Ranjit Garh";
gdate = "2012-03-25";
gid = 1;
id = 1;
p1nets = "3,3,4,4,2,2,2,4,3,3,4,2,5,3,5,4,3,4||27||33||60";
p1nh = "BP||14||1";
p1score = "4,4,5,5,3,3,3,5,4,4,5,3,5,3,6,4,4,5||36||39||75||61";
p1spoints = "3,3,3,2,3,4,3,3,3,3,2,4,1,2,2,1,3,3||27||21||48";
p2nets = "4,3,4,4,3,4,2,5,4,4,3,3,4,3,6,3,4,4||33||34||67";
p2nh = "DG||18||2";
p2score = "5,4,5,5,4,5,3,6,5,5,4,4,5,4,7,4,5,5||42||43||85||67";
p2spoints = "2,3,3,2,2,2,3,2,2,2,3,3,2,2,1,2,2,3||21||20||41";
p3nets = "4,3,6,4,2,3,2,4,5,4,3,5,6,3,4,3,5,6||33||39||72";
p3nh = "NM||24||3";
p3score = "6,5,7,6,4,5,3,6,6,5,5,6,7,4,5,4,6,7||48||49||97||73";
p3spoints = "2,3,1,2,3,3,3,3,1,2,3,1,0,2,3,2,1,1||21||15||36";
p4nets = "3,4,5,4,2,3,3,5,3,4,3,5,4,3,5,3,4,6||32||37||69";
p4nh = "KS||20||4";
p4score = "5,5,6,5,3,5,4,6,4,5,5,6,5,4,6,4,5,7||43||47||90||70";
p4spoints = "3,2,2,2,3,3,2,2,3,2,3,1,2,2,2,2,2,1||22||17||39";
},
{
cn = "Eden Garden";
gdate = "2012-03-26";
gid = 2;
id = 2;
p1nets = "3,2,5,3,1,2,2,4,4,3,4,3,5,2,5,3,3,5||26||33||59";
p1nh = "NM||24||3";
p1score = "5,4,6,5,3,4,3,6,5,4,5,4,6,3,6,4,4,6||41||42||83||59";
p1spoints = "3,4,2,3,4,4,3,3,2,3,2,3,1,3,2,2,3,2||28||21||49";
p2nets = "2,4,5,3,2,4,3,4,4,4,3,3,5,2,5,3,4,4||31||33||64";
p2nh = "KS||20||4";
p2score = "4,5,6,4,3,6,4,5,5,5,4,4,6,3,6,4,5,5||42||42||84||64";
p2spoints = "4,2,2,3,3,2,2,3,2,2,3,3,1,3,2,2,2,3||23||21||44";
p3nets = "3,4,5,5,3,5,4,5,5,5,4,5,5,4,5,3,5,6||39||42||81";
p3nh = "AK||18||5";
p3score = "4,5,6,6,4,6,5,6,6,6,5,6,6,5,6,4,6,7||48||51||99||81";
p3spoints = "3,2,2,1,2,1,1,2,1,1,2,1,1,1,2,2,1,1||15||12||27";
p4nets = "2,4,5,4,3,3,3,5,5,4,3,5,4,3,5,4,5,6||34||39||73";
p4nh = "KR||20||6";
p4score = "4,5,6,5,4,5,4,6,6,5,4,6,5,4,6,5,6,7||45||48||93||73";
p4spoints = "4,2,2,2,2,3,2,2,1,2,3,1,2,2,2,1,1,1||20||15||35";
},
)
the way i want
normalscoringarray....(
id = 1,
p1nets = "3,3,4,4,2,2,2,4,3,3,4,2,5,3,5,4,3,4||27||33||60",
p1nh = "BP||14||1",
p1score = "4,4,5,5,3,3,3,5,4,4,5,3,5,3,6,4,4,5||36||39||75||61",
p1spoints = "3,3,3,2,3,4,3,3,3,3,2,4,1,2,2,1,3,3||27||21||48",
p2nets = "4,3,4,4,3,4,2,5,4,4,3,3,4,3,6,3,4,4||33||34||67",
p2nh = "DG||18||2",
p2score = "5,4,5,5,4,5,3,6,5,5,4,4,5,4,7,4,5,5||42||43||85||67",
p2spoints = "2,3,3,2,2,2,3,2,2,2,3,3,2,2,1,2,2,3||21||20||41",
p3nets = "4,3,6,4,2,3,2,4,5,4,3,5,6,3,4,3,5,6||33||39||72",
p3nh = "NM||24||3",
p3score = "6,5,7,6,4,5,3,6,6,5,5,6,7,4,5,4,6,7||48||49||97||73",
p3spoints = "2,3,1,2,3,3,3,3,1,2,3,1,0,2,3,2,1,1||21||15||36",
p4nets = "3,4,5,4,2,3,3,5,3,4,3,5,4,3,5,3,4,6||32||37||69";
p4nh = "KS||20||4";
p4score = "5,5,6,5,3,5,4,6,4,5,5,6,5,4,6,4,5,7||43||47||90||70",
p4spoints = "3,2,2,2,3,3,2,2,3,2,3,1,2,2,2,2,2,1||22||17||39",
)
NSMutableArray *remainingArray = [[NSMutableArray alloc]init];
for (int i=0; i<[normalscoringarray count]; i++)
{
NSMutableDictionary *mutableDictionary = [[NSMutableDictionary alloc] initWithDictionary:[normalscoringarray objectAtIndex:i]]; //each time alloc and init with contents of normalscoringarray
//Remove values for keys
[mutableDictionary removeObjectForKey:#"cn"];
[mutableDictionary removeObjectForKey:#"gdate"];
[mutableDictionary removeObjectForKey:#"gid"];
[remainingArray addObject:mutableDictionary]; //add mutableDictionary to remainingArray
[mutableDictionary release]; //free mutableDictionary
}
//finally print contents of remainingArray
NSLog(#"normalscoringarray:\n%#",[remainingArray description]);
Hope this would be help you!
for (int i=0; i<[normalscoringarray count]; i++)
{
NSMutableDictionary *dictionary = [NSMutableDictionary dictionaryWithDictionary:[normalscoringarray objectAtIndex:i]];
[dictionary removeObjectForKey:#"cn"];
[dictionary removeObjectForKey:#"gdate"];
[dictionary removeObjectForKey:#"gid"];
[normalscoringarray replaceObjectAtIndex:i withObject:dictionary];
}

Multidimensional array issues

I am creating a multidimensional array for sections / rows based on json data from our API. Looking at the logs the adding rows and sections look good but when I nslog the sections it only shows a bunch of the last object. It seems like the rows are not being added to sections.
What am I doing wrong to make the last API object show in the sections x times? x represents the json count. self.appointments is an NSArray *
According to objective-c multi-dimensional array it should work.
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
NSMutableArray *sections = [[NSMutableArray alloc] init];
NSMutableArray *rows = [[NSMutableArray alloc] init];
NSString *lastDate = nil;
for (NSDictionary *dict in [json objectForKey:#"data"]) {
NSString *date = [dict objectForKey:#"date"];
NSLog(#"Dates: %# - %#", date, lastDate);
if (date != lastDate) {
if (lastDate == nil) {
NSLog(#"Adding Row 1");
[rows addObject:dict];
lastDate = date;
} else {
NSLog(#"Adding Section 1");
NSLog(#"Adding #rows %i",[rows count]);
[sections addObject:rows];
[rows removeAllObjects];
NSLog(#"Adding Row 2 %#",[dict objectForKey:#"start_time"]);
[rows addObject:dict];
lastDate = date;
}
} else {
NSLog(#"Adding Row 3");
[rows addObject:dict];
}
}
if (rows) {
NSLog(#"Adding Section 2");
NSLog(#"Adding #rows %i",[rows count]);
[sections addObject:rows];
}
NSLog(#"Sections: %#", sections);
self.appointments = [sections mutableCopy]; //I have also tried self.appointments = sections
sections = nil;
rows = nil;
Logs show
Sections: (
(
{
abbrev = "";
account = "";
"addOnService_id" = "";
alert = "";
"appt_id" = 1839384;
"appt_id_unique" = 1839384;
"appt_status_description" = "";
"appt_status_type" = "";
"c_id" = 47;
cost = "0.00";
"coupon_id" = "";
"creation_emp_id" = 2288;
"creation_timestamp" = 201111040717;
"customer_id" = 0;
"customer_notes" = "";
"customer_package_id" = "";
date = 20121228;
"employee_id" = 2288;
"employee_notes" = "";
employer = "";
"end_time" = 570;
"first_name" = "";
"history_id" = 1830959;
key = 134;
"last_emp_id" = 2288;
"last_name" = "";
"last_timestamp" = 201111040717;
"lead_description" = "";
"link_id" = 0;
"location_name" = "Telephonic Setup/Training";
"make_id" = "";
"middle_name" = "";
"model_id" = "";
"model_year" = "";
name = "My Name ";
odometer = "";
"other_vehicle" = "";
"package_name" = "";
"payment_type_description" = "";
"payment_type_id" = "";
"pet_id" = "";
"po_number" = "";
reason = "B.O.B";
"rebook_id" = "";
"recur_id" = 20954;
"rep_id" = "";
"room_id" = 0;
"room_name" = "";
service = "";
"service_id" = 0;
"service_time_description" = "";
spots = 1;
"staff_screen_name" = "John Smith";
"staff_type_id" = 0;
"start_time" = 540;
"status_id" = 0;
tip = "";
"type_id" = 8;
vin = "";
}
),
(
{
abbrev = "";
account = "";
"addOnService_id" = "";
alert = "";
"appt_id" = 1839384;
"appt_id_unique" = 1839384;
"appt_status_description" = "";
"appt_status_type" = "";
"c_id" = 47;
cost = "0.00";
"coupon_id" = "";
"creation_emp_id" = 2288;
"creation_timestamp" = 201111040717;
"customer_id" = 0;
"customer_notes" = "";
"customer_package_id" = "";
date = 20121228;
"employee_id" = 2288;
"employee_notes" = "";
employer = "";
"end_time" = 570;
"first_name" = "";
"history_id" = 1830959;
key = 134;
"last_emp_id" = 2288;
"last_name" = "";
"last_timestamp" = 201111040717;
"lead_description" = "";
"link_id" = 0;
"location_name" = "Telephonic Setup/Training";
"make_id" = "";
"middle_name" = "";
"model_id" = "";
"model_year" = "";
name = "My Name ";
odometer = "";
"other_vehicle" = "";
"package_name" = "";
"payment_type_description" = "";
"payment_type_id" = "";
"pet_id" = "";
"po_number" = "";
reason = "B.O.B";
"rebook_id" = "";
"recur_id" = 20954;
"rep_id" = "";
"room_id" = 0;
"room_name" = "";
service = "";
"service_id" = 0;
"service_time_description" = "";
spots = 1;
"staff_screen_name" = "John Smith";
"staff_type_id" = 0;
"start_time" = 540;
"status_id" = 0;
tip = "";
"type_id" = 8;
vin = "";
}
), ... over and over again.
With:
[sections addObject:rows];
[rows removeAllObjects];
[rows addObject:dict];
you keep adding the same rows object not a new one and keep changing the contents so the last contents display each time.
Try:
[sections addObject:[rows mutableCopy]];
[rows removeAllObjects];
[rows addObject:dict];
That way a new row is added each time.
Perhaps better yet:
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
NSMutableArray *sections = [NSMutableArray array];
NSMutableArray *rows = [NSMutableArray array];
NSString *lastDate = nil;
for (NSDictionary *dict in [json objectForKey:#"data"]) {
NSString *date = [dict objectForKey:#"date"];
if ([date isEqualToString:lastDate] == NO) {
if (lastDate == nil) {
[rows addObject:dict];
lastDate = date;
} else {
[sections addObject:rows];
rows = [NSMutableArray array];
[rows addObject:dict];
lastDate = date;
}
[rows addObject:dict];
}
}
if (rows) {
[sections addObject:rows];
}
self.appointments = sections;
Note that assigning 0 to an object does not release it, release does. But if you use convenience methods to create objects then they are autoreleased and no further action is necessary to release them.
Also note that when comparing strings one needs to use the isEqualToString: method to compare the contents, just using the = or != only compares the address of the strings.

Retrieve value from NSDictionnary

I have a NSDictionary and I don't know how I can retrieve the value of "#mode" and "#route" (this is from a json response).
How I can do that?
Thanks!
This is the NSDictionary :
{
"#mode" = WALK;
"#route" = "Rue University";
distance = "17.513516908661757";
duration = 13000;
endTime = "2011-04-29T12:08:59.395-04:00";
from = {
geometry = "{\"type\": \"Point\", \"coordinates\": [-73.57120386807037,45.503820214186405]}";
lat = "45.503820214186405";
lon = "-73.57120386807037";
name = "Rue University";
};
legGeometry = {
length = 3;
points = "{mvtG`k``MOd#ME";
};
startTime = "2011-04-29T12:08:46.395-04:00";
steps = {
walkSteps = (
{
absoluteDirection = NORTHWEST;
becomes = false;
distance = "17.513516908661757";
elevation = "";
lat = "45.503820214186405";
lon = "-73.57120386807037";
stayOn = false;
streetName = "Rue University";
},
{
absoluteDirection = NORTH;
becomes = true;
distance = "0.0";
elevation = "";
lat = "45.50390573910769";
lon = "-73.57139257694809";
relativeDirection = RIGHT;
stayOn = false;
streetName = "street transit link";
}
);
};
to = {
geometry = "{\"type\": \"Point\", \"coordinates\": [-73.571363,45.503971]}";
lat = "45.503971";
lon = "-73.571363";
name = "Station McGill";
stopId = 32;
};
Looks like object at index 0 is a NSDictionary.
NSDictionary *dict = [array objectAtIndex:0];
NSString *route = [dict objectForKey:#"#route"];
Try this:
[myDictionary objectForKey:#"#mode"];
Given an NSDictionary dict, you can retrieve the value corresponding to the key #mode using:
[dict objectForKey:#"#mode"];
Two possibilities:
1) The NSMutableArray is an array of JSON formatted strings.
2) The NSMutableArray is an array of NSDictionaries.
If its #1, then you need to use the Cocoa JSON stuff to decode the JSON string into an NSDictionary:
NSDictionary* aDict = [[myArray objectAtIndex:0] JSONValue];
OR
NSDictionary* aDict = [theArray objectAtIndex:0];
The JSON stuff is available here http://code.google.com/p/json-framework/
Then this should work:
NSString* theMode = [aDict objectForKey:#"#mode"];
NSLog(#"the mode is %#, as read from the dictionary %#", theMode, aDict);
--Tom