NSDictionary with ordered keys
The solution of having an associated NSMutableArray of keys isn’t so bad. It avoids subclassing NSDictionary, and if you are careful with writing accessors, it shouldn’t be too hard to keep synchronised.
The solution of having an associated NSMutableArray of keys isn’t so bad. It avoids subclassing NSDictionary, and if you are careful with writing accessors, it shouldn’t be too hard to keep synchronised.
I believe you are misinterpreting the JSON format for key values. You should store your string as NSString *jsonString = @”{\”ID\”:{\”Content\”:268,\”type\”:\”text\”},\”ContractTemplateID\”:{\”Content\”:65,\”type\”:\”text\”}}”; NSData *data = [jsonString dataUsingEncoding:NSUTF8StringEncoding]; id json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil]; Now if you do following NSLog statement NSLog(@”%@”,[json objectForKey:@”ID”]); Result would be another NSDictionary. { Content = 268; type = text; }
Just use NSArray*keys=[dict allKeys]; In general, if you wonder if a specific class has a specific method, look up Apple’s own documentation. In this case, see NSDictionary class reference. Go through all the methods. You’ll discover many useful methods that way.
You looking for this guy: [NSMutableDictionary addEntriesFromDictionary:] Make sure your UITableView dictionary is an NSMutableDictionary! Check it here
It should work – as long as the data variable is actually an array containing a dictionary with the key SPORT NSArray *data = [NSArray arrayWithObject:[NSMutableDictionary dictionaryWithObject:@”foo” forKey:@”BAR”]]; NSArray *filtered = [data filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@”(BAR == %@)”, @”foo”]]; Filtered in this case contains the dictionary. (the %@ does not have to be quoted, this is done … Read more
A mutable dictionary can be changed, i.e. you can add and remove objects. An immutable is fixed once it is created. create and add: NSMutableDictionary *dict = [[NSMutableDictionary alloc]initWithCapacity:10]; [dict setObject:[NSNumber numberWithInt:42] forKey:@”A cool number”]; and retrieve: int myNumber = [[dict objectForKey:@”A cool number”] intValue];
Just ask it for the objectForKey:@”b”. If it returns nil, no object is set at that key. if ([xyz objectForKey:@”b”]) { NSLog(@”There’s an object set for key @\”b\”!”); } else { NSLog(@”No object set for key @\”b\””); } Edit: As to your edited second question, it’s simply NSUInteger mCount = [xyz count];. Both of these … Read more
It looks like you are passing an NSString parameter where you should be passing an NSData parameter: NSError *jsonError; NSData *objectData = [@”{\”2\”:\”3\”}” dataUsingEncoding:NSUTF8StringEncoding]; NSDictionary *json = [NSJSONSerialization JSONObjectWithData:objectData options:NSJSONReadingMutableContainers error:&jsonError];
Your root json object is not a dictionary but an array: [{“id”: “1”, “name”:”Aaa”}, {“id”: “2”, “name”:”Bbb”}] This might give you a clear picture of how to handle it: NSError *e = nil; NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData: data options: NSJSONReadingMutableContainers error: &e]; if (!jsonArray) { NSLog(@”Error parsing JSON: %@”, e); } else { for(NSDictionary … Read more
NSDictionary -> NSData: NSData *myData = [NSKeyedArchiver archivedDataWithRootObject:myDictionary]; NSData -> NSDictionary: NSDictionary *myDictionary = (NSDictionary*) [NSKeyedUnarchiver unarchiveObjectWithData:myData];