nsdictionary

Is NSDictionary key order guaranteed the same as initialized if it never changes?

江枫思渺然 提交于 2019-11-28 12:19:43
I've run into the same problem as found in this question . However, I have a follow-up question. I seem to be in the same situation as the original asker: I have a plist with a hierarchy of dictionaries that define a configuration screen. These are not mutable and will stay the same throughout the application. Since the original discussion seems to focus on problems arising from mutating the dictionary, I must ask for comfirmation: is the order of a dictionary guaranteed the same as they are in the plist, i.e. as it is read (with initWithContentsOfFile)? Can I use allKeys on it in this case to

Converting NSObject to NSDictionary

左心房为你撑大大i 提交于 2019-11-28 11:05:19
Hello I a class of type NSObject: ProductDetails *details = [[ProductDetails alloc] init]; details.name = @"Soap1"; details.color = @"Red"; details.quantity = 4; I want to pass the "details" object to a dictionary. I did, NSDictionary *dict = [NSDictionary dictionaryWithObject:details forKey:@"details"]; I am passing this dict to another method which performs a check on JSONSerialization: if(![NSJSONSerialization isValidJSONObject:dict]) And I am getting a crash on this check. Am I doing anything wrong here? I know that the details I am getting is a JSON object and I am assigning it to the

iPhone app crash [__NSCFString objectForKey:] unrecognized selector sent to instance

女生的网名这么多〃 提交于 2019-11-28 10:50:42
问题 I am trying to add in tempArray only even number of data.localBookmarks is array of dictionary. Here is my code : currentIndex = indexPath.row; for (NSDictionary *dict in localBookmarks) { if (currentIndex % 2 == 0 && currentIndex <= [localBookmarks count]) { [tempArray addObject:[dict objectForKey:@"firstName"]]; } currentIndex++; } NSLog(@"tempArray %@",tempArray); cell.textLabel.text = [tempArray objectAtIndex:indexPath.row]; return cell; my app crash on [tempArray addObject:[dict

NSDictionary keys sorted by value numerically

馋奶兔 提交于 2019-11-28 10:48:05
I store names as keys and scores as values into an NSDictionary for saving in NSUserDefaults . I then want to get back the keys sorted by score, but I can't seem to sort them numerically, only by string. The list of scores 100, 50, 300, 200, 500, for example, gives me 100, 200, 300, 50, 500. Can this be done or do I need to go about this differently? NSString *defaultNames[] = {@"Matt", @"Terry",@"Jessica",@"Sean",nil}; NSNumber *defaultScores[] = {@"600", @"500",@"100",@"50", nil}; NSDictionary *newScoreDict = [NSDictionary dictionaryWithObjects:(id *)defaultScores forKeys:(id *)defaultNames

A question on how to Get data from plist & how should it be layout

血红的双手。 提交于 2019-11-28 10:23:08
问题 This is a follow up question on my first queries regarding retrieving data on plist. Right now i have manage to detect users touches made on my view with random image call out (thanks to phytonquick). CGPoint currentTouchLocation = [currentTouch locationInView:self]; Im having trouble now on how to compare the value i got from the users touches made on the random image in the view to the one save inside the plist data with the same name as the random image the users touches. I know how to

creating JSON format in Objective C

半腔热情 提交于 2019-11-28 10:18:41
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"];

Swift JSON parsing with Dictionary with Array of Dictionaries

落花浮王杯 提交于 2019-11-28 09:43:59
问题 I am a beginner in iOS development with Swift language. I have a JSON file contains the data as below. { "success": true, "data": [ { "type": 0, "name": "Money Extension", "bal": "72 $", "Name": "LK_Mor", "code": "LK_Mor", "class": "0", "withdraw": "300 $", "initval": "1000 $" }, { }, { }, ] } I want to parse this file and have to return the dictionary which contain the data in the JSON file. This is the method I wrote. enum JSONError: String, ErrorType { case NoData = "ERROR: no data" case

Remove nested key from dictionary

本小妞迷上赌 提交于 2019-11-28 07:24:54
问题 Let's say I have a rather complex dictionary, like this one: let dict: [String: Any] = [ "countries": [ "japan": [ "capital": [ "name": "tokyo", "lat": "35.6895", "lon": "139.6917" ], "language": "japanese" ] ], "airports": [ "germany": ["FRA", "MUC", "HAM", "TXL"] ] ] I can access all fields with if let .. blocks optionally casting to something that I can work with, when reading. However, I am currently writing unit tests where I need to selectively break dictionaries in multiple ways. But I

Best way to save to nsuserdefaults for custom class?

有些话、适合烂在心里 提交于 2019-11-28 05:59:33
If I have a custom class Person which has three variables (which are propertized and synthesized): NSString* theName; float* theHeight; int theAge; Person instances are stored in an NSArray 'Group'. There is only one Group. What is the best way of storing and loading the Group in NSUserDefaults? (bearing in mind that float and int are not legal for NSUserDefaults) @Brad Smith's answer is not complete, and even is incorrect in some sense. We have to use NSKeyedArchiver and NSKeyedUnarchiver as follows: Make your class (for example in this case Person ) to conform to protocol NSCoding and

Replace a value in NSDictionary in iPhone

僤鯓⒐⒋嵵緔 提交于 2019-11-28 04:42:25
I have a array (dataArray) of NSDictionary "item". It has datas like "david" for key "name" and "85" for key "marks" etc for 5 students. I want to replace the mark of david to 90 with respect to the array index value (ie 0 for dictionary containing david and 85). How can I do it? The code for content in array is [item setobject:name forkey:@"Name"]; [item setobject:mark forkey:@"Marks"]; [dataArray addOject:item] The above code goes inside parsing, so i have array with 5 objects (students), their name and marks, now I want to replace the mark of the first object in the dataArray. donkim Here's