nsdictionary

AFNetworking 2.0 - use responseObject as NSDictionary

删除回忆录丶 提交于 2019-12-02 22:36:25
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; [manager GET:@"http://example.com/resources.json" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) { NSLog(@"JSON: %@", responseObject); } failure:^(AFHTTPRequestOperation *operation, NSError *error) { NSLog(@"Error: %@", error); }]; this is the recommended way to send GET request in AFNetworking 2.0. I want to get the value of a specific key in the json, so I want to use responseObject as NSDictionary . this is what I was trying: NSError *jsonError = nil; NSDictionary *json =

NSDictionary sort by keys as floats

孤街浪徒 提交于 2019-12-02 22:20:43
basically I have an NSDictionary with keys and values. The keys are all numbers, but at the moment they are strings. I want to be able to compare them as numbers in order to sort them. eg: If I have a Dictionary like this: { "100" => (id)object, "20" => (id)object, "10" => (id)object, "1000" => (id)object, } I want to be able to sort it like this: { "10" => (id)object, "20" => (id)object, "100" => (id)object, "1000" => (id)object, } Any ideas? Thanks Tom Not sure what you are up to – dictionaries are inherently unsorted, there is no stable key ordering in the default implementation. If you

NSDictionary Key For Value/Object?

China☆狼群 提交于 2019-12-02 21:32:40
Can we get the key for an object in an NSDictionary by passing a particular value or object? -[NSDictionary allKeysForObject:] returns an NSArray of all the keys whose objects match the passed object, where "match" is determined by isEqual: . To answer you question in a more specific manner, use the following to get a key for a particular object: NSString *knownObject = @"the object"; NSArray *temp = [dict allKeysForObject:knownObject]; NSString *key = [temp lastObject]; //"key" is now equal to the key of the object you were looking for wzbozon This is how you can get a first key for object

How should I structure my .plist array and dictionary?

心已入冬 提交于 2019-12-02 21:01:16
问题 I have UITableView that will have several child UITableViews which may also have 1 or 2 childs each. I want to create a plist to keep track and structure all the cell names and stuff. Man view -> Child -> Child -> Child -> Detail What is the best way to structure this? Array or Dictionary? 回答1: You will probably want an array of dictionaries, where each array represents a row and the dictionary contains info about the cell, and potentially the child table views. Consider modeling this off the

Check key exists in NSDictionary

与世无争的帅哥 提交于 2019-12-02 21:00:19
how can I check if this exists?: [[dataArray objectAtIndex:indexPathSet.row] valueForKey:@"SetEntries"] I want to know whether this key exists or not. How can I do that? Thank you very much :) EDIT: dataArray has Objects in it. And these objects are NSDictionaries. John Parker I presume that [dataArray objectAtIndex:indexPathSet.row] is returning an NSDictionary , in which case you can simply check the result of valueForKey against nil. For example: if ([[dataArray objectAtIndex:indexPathSet.row] valueForKey:@"SetEntries"] != nil) { // The key existed... } else { // No joy... } Miles Alden So

Passing data from local file using json

走远了吗. 提交于 2019-12-02 20:40:51
I am trying to pass data to labels from my JSON file onto a simple ViewController but I don't know where to actually pass that data. Would I be able to just add to my setDataToJson method or would I add the data in my viewDidLoad method? here is my code @interface NSDictionary(JSONCategories) +(NSDictionary*)dictionaryWithContentsOfJSONString:(NSString*)fileLocation; @end @implementation NSDictionary(JSONCategories) +(NSDictionary*)dictionaryWithContentsOfJSONString:(NSString*)fileLocation{ NSData* data = [NSData dataWithContentsOfFile:fileLocation]; __autoreleasing NSError* error = nil; id

How can I store a UIImage in an NSDictionary?

泪湿孤枕 提交于 2019-12-02 20:34:09
How can I store a UIImage in an NSDictionary? Yes you can: NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithCapacity:1]; UIImage *img = ...; [dict setObject:img forKey:@"aKeyForYourImage"]; UIImage *imgAgain = [dict objectForKey:@"aKeyForYourImage"]; RyanTCB OK I had this problem recently and the above solutions never worked for me. So if someone else finds their way here to solve the same problem, here is how it's done NSDictionary *imageDictionary = @{ kImageKey: UIImageJPEGRepresentation(/*INSERT YOUR UIImage HERE*/,0.1) }; // the 0.1 represents the compression quality 0.0

Add objects to NSMutable array with grouping

泪湿孤枕 提交于 2019-12-02 19:02:34
问题 I want my NSArray sampleData to receive actual data from parse.com database assuming like this: self.sampleData = @[ @{ @"date": @"12/5/2014", @"group": @[ @{ @"text": @"post1", @"location": @"x,y" }, @{ @"text": @"post2", @"location": @"x,y" }, @{ @"text": @"post3", @"location": @"x,y" }, @{ @"text": @"post4", @"location": @"x,y" }, @{ @"text": @"post5", @"location": @"x,y" } ] }, @{ @"date": @"12/3/2014", @"group": @[ @{ @"text": @"post6", @"location": @"x,y" }, @{ @"text": @"post7", @

Could not cast value of type '__NSArrayM' to 'NSDictionary'

让人想犯罪 __ 提交于 2019-12-02 19:02:07
问题 I have a json.I am trying to parse that with that code.But its says Could not cast value of type '__NSArrayM' to 'NSDictionary' do { let dataDictionary: NSDictionary = try NSJSONSerialization.JSONObjectWithData(responseObject as! NSData, options: NSJSONReadingOptions.MutableContainers) as! NSDictionary // <------ Error if let customerArray = dataDictionary.valueForKey("cart") as? NSArray { for js in customerArray { let nameArray = js.valueForKey("name") let idArray = js.valueForKey("id") } }

Storing NSManagedObject in a dictionary (NSDictionary)

为君一笑 提交于 2019-12-02 18:41:27
I have a custom class, which is a subclass of NSManagedObject . I would like to store it in a dictionary, but when trying to do so I receive a Property list invalid for format: 200 error. Here is how I try to create the dictionary: NSDictionary *dictionary = [NSDictionary dictionaryWithObject: voiceMemo forKey:@"voiceMemo"]; Same result when trying NSDictionary *dictionary = [NSDictionary dictionaryWithObject: (NSData *) voiceMemo forKey:@"voiceMemo"]; It works, however, when trying to save the individual attributes separately: NSDictionary *dictionary = [NSDictionary dictionaryWithObject: