nsmutabledictionary

How to insert a new key with value in dictionary

邮差的信 提交于 2019-11-29 09:04:57
I want to insert a key with a corresponding value in an existing dictionary... I am able to set values for existing keys in the dictionary, but I am not able to add a new key value... Any help would be appreciated. Use NSMutableDictionary NSMutableDictionary *yourMutableDictionary = [NSMutableDictionary alloc] init]; [yourMutableDictionary setObject:@"Value" forKey:@"your key"]; Update for Swift: The following is the exact swift replica for the code mentioned above var yourMutableDictionary = NSMutableDictionary() yourMutableDictionary.setObject("Value", forKey: "Key") But i would suggest you

Convert NSDictionary to Swift Dictionary

天涯浪子 提交于 2019-11-28 21:03:04
Now I know that when swift compiles it just makes a NSDictionary, but the NSDictionary and Swift dictionaries have different syntax. Is there a way (through a loop or something) to convert a NSDictionary to a swift dictionary of the same type for <key, value> ? OR Is there a way to convert this to a Swift dictionary instead of NSDictionary? let jsonDict = NSJSONSerialization.JSONObjectWithData(jsonData, options: nil, error: &error) as NSDictionary use: let jsonDic = NSJSONSerialization.JSONObjectWithData(jsonData, options: NSJSONReadingOptions.MutableContainers, error: &error) as Dictionary

Observing NSMutableDictionary changes

左心房为你撑大大i 提交于 2019-11-28 19:24:58
Is it possible to observe (subscribe to) changes to values stored under different keys in an NSMutableDictionary? In my case the keys would already exist when the subscription is initiated, but the values change and I would like to be notified in this case. I would like the key of the changed value in the notification. I assume that if my dictionary keys were all NSString instances I could just subscribe to each key path individually. But what if my keys are non-strings? Am I out of luck in that case? That's an interesting idea. I can't find anything in NSDictionary or NSDictionaryController

How to assign values from NSMutableDictionary to NSArray

我们两清 提交于 2019-11-28 13:58:54
问题 I am doing JSON parsing and I want to show my parsed data in a UITableView . For that, I am trying to assign parsed data from NSMutableDictionary to NSArray to show in the table view but the array returns null . Here my array returns null value; NSMutableDictionary *tempDict1; NSArray *arr = [[tempDict1 valueForKey:@"rates"] componentsSeparatedByString:@";"]; code - (void)connectionDidFinishLoading:(NSURLConnection *)connection { [connection release]; NSString *responseString = [[NSString

iPhone - Writing NSMutableDictionary to file

社会主义新天地 提交于 2019-11-28 13:13:35
I'm having trouble in writing mutable dictionary to a file. Here's the code that I'm writing. I'm reading the file like below: (first time when app is ran, it won't have any data) NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString* documentsDirectory = [paths objectAtIndex:0]; self.favSaveFilePath = [[NSString alloc] initWithString:[documentsDirectory stringByAppendingPathComponent:@"Favorites.plist"]]; if([fm fileExistsAtPath:favSaveFilePath]) { favoriteJokesDictionary = [[NSMutableDictionary alloc] initWithContentsOfFile:self

how to write NSMutableDictionary into Plist

余生长醉 提交于 2019-11-28 05:12:46
问题 can any one help me with this how to write a NSMutableDictionary into a plist.... thanks in advance.. 回答1: Make sure the pList file you are writing to is located in a legal place to edit it, for example Documents in the apps sandbox. Then find the path to that location (if there is an existing pList file, it will overwrite), and use: [myDictionary writeToFile:path atomically:YES]; Write how far you are in the process, and maybe some code / error-message... 回答2: [yourDict writeToFile:filePath

NSMutableDictionary is adding quotes to keys and values - why?

二次信任 提交于 2019-11-28 04:19:57
问题 I'm trying to add some additional key/value pairs to an NSMutableDictionary, using: Tag *tag1 = [results1 objectAtIndex:0]; [resultsDict setObject:[tag1 retrieveTextUpToDepth:1] forKey:@"image_url"]; Tag *tag2 = [results2 objectAtIndex:0]; [resultsDict setValue:[tag2 retrieveTextUpToDepth:1] forKey:@"majority"]; This adds the k/v pairs with no problem, except when I come to retrieve them, some of the values have been wrapped with double quotes: po extendedDataDictionary: "image_url" = "

Why NSMutableDictionary don't want write to file?

百般思念 提交于 2019-11-28 01:38:44
问题 - (void)viewDidLoad { [super viewDidLoad]; if ([[NSFileManager defaultManager] fileExistsAtPath:pathString]) { infoDict = [[NSMutableDictionary alloc] initWithContentsOfFile:pathString]; } else { infoDict = [[NSMutableDictionary alloc]initWithObjects:[NSArray arrayWithObjects:@"BeginFrame",@"EndFrame", nil] forKeys:[NSArray arrayWithObjects:[NSNumber numberWithBool:YES],[NSNumber numberWithBool:YES], nil]]; if ([infoDict writeToFile:pathString atomically:YES]) { NSLog(@"Created"); } else {

How do you store “int” values in an NSMutableArray* or NSMutableDictionary*? Chronic problems with JSON data that come in as integers.

纵饮孤独 提交于 2019-11-27 21:50:49
问题 How do you store "int" values in an NSMutableArray or NSMutableDictionary ? Chronic problems with JSON data that come in as integers. Should I try to store these integers as NSNumber objects or just as strings that contain the integer? How dangerous is it to do a "raw" (int) conversion on the pointers if I know the values will always be Natural Numbers (numbers>=0 including zero for null.) The integers I'm always working with are Foreign Key Id's from a database. 回答1: Use NSNumber like this:

how can I sort NSMutableDictionary with keys value?

我与影子孤独终老i 提交于 2019-11-27 14:59:10
问题 I have a NSMutableDictionary with string keys and every key has its own array. I want to re-sort the dictionary with keys value alphabetically. How can I do this? 回答1: A dictionary is unsorted by definition. For iterating over the keys in a specific order, you can sort an array containing the keys of the dictionary. NSArray *keys = [theDictionary allKeys]; NSArray *sortedKeys = [keys sortedArrayUsingSelector:@selector(compareMethod:)]; There are several other sorted... methods, e.g. sorting