nsdictionary

Deep combine NSDictionaries

流过昼夜 提交于 2019-11-26 22:54:21
问题 I need to merge two NSDictionary s into one provided that if there are dictionaries within the dictionaries, they are also merged. More or less like jQuery's extend function. 回答1: NSDictionary+Merge.h #import <Foundation/Foundation.h> @interface NSDictionary (Merge) + (NSDictionary *) dictionaryByMerging: (NSDictionary *) dict1 with: (NSDictionary *) dict2; - (NSDictionary *) dictionaryByMergingWith: (NSDictionary *) dict; @end NSDictionary+Merge.m #import "NSDictionary+Merge.h"

Keeping dictionary keys of JSON Object in order in Objective C

柔情痞子 提交于 2019-11-26 22:26:52
问题 I'm using the JSON-FRAMEWORK in objective C to parse a JSON object. When I call [jsonString JSONValue], I get back a dictionary, but the keys aren't in the same order as the JSON Object I'm parsing. Is there anyway to keep this order the same? 回答1: In JSON objects, by definition , the order of the key-value pairs is not meaningful. The specification allows a JSON producer to permute them any way it want, even at random -- and does not require a parser to preserve the ordering. RFC 4627 says:

SBJsonWriter Nested NSDictionary

纵然是瞬间 提交于 2019-11-26 21:44:48
问题 I'm trying to serialize an objc object with Json to send it to a server. That same server sends the following on GET for this object type: { "TypeProperties":[ {"Key":"prop 0","Value":"blah 0"}, {"Key":"prop 1","Value":"blah 1"}, {"Key":"prop 2","Value":"blah 2"}, {"Key":"prop 3","Value":"blah 3"} ], "ImageURls":[ {"Key":"url 0","Value":"blah 0"}, {"Key":"url 1","Value":"blah 1"}, {"Key":"url 2","Value":"blah 2"}, {"Key":"url 3","Value":"blah 3"} ] } SBJsonWriter produces the following for

How do I load a plist file from disk as a NSDictionary on iOS?

感情迁移 提交于 2019-11-26 21:31:46
问题 I want to load the plist file from disk (documents, application cache, ...) not from a resource bundle. 回答1: You can load a plist from any accessible file path with -initWithContentsOfFile: or +dictionaryWithContentsOfFile: Load a plist from a file, and create the file if it did not exist: NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); self.plistFile = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"example.plist"]; self.plist = [

writing NSDictionary to plist in my app bundle

为君一笑 提交于 2019-11-26 20:44:00
I'm trying to write an NSDictionary to a plist but when I open the plist no data has been written to it. From the log my path looks correct and my code is pretty standard. Any ideas? NSArray *keys = [NSArray arrayWithObjects:@"key1", @"key2", @"key3", nil]; NSArray *objects = [NSArray arrayWithObjects:@"value1", @"value2", @"value3", nil]; NSDictionary *dictionary = [NSDictionary dictionaryWithObjects:objects forKeys:keys]; for (id key in dictionary) { NSLog(@"key: %@, value: %@", key, [dictionary objectForKey:key]); } NSString *path = [[NSBundle mainBundle] pathForResource:@"FormData" ofType:

Save NSDictionary to plist

萝らか妹 提交于 2019-11-26 20:17:48
I am using the code found in this post: Mulitple Arrays From Plist , with the same plist formatting. This works successfully, however I do not know how to save the created arrays back into a plist, in the same format. How would I achieve this? EDIT: It is not so much the saving that I need, but the forming of the data to save. The plist is an array of dictionaries with strings and their corresponding keys. All the strings with a certain key are put into an array of their own. How would I put that array back into the correct positions in the array of dictionaries, ready to save? Jhaliya Don't

Can i sort the NSDictionary on basis of key in Objective-C?

試著忘記壹切 提交于 2019-11-26 20:04:59
Can I sort the NSDictionary on basis of key? Max Seelemann You can sort the keys and then create an NSMutableArray by iterating over them. NSArray *sortedKeys = [[dict allKeys] sortedArrayUsingSelector: @selector(compare:)]; NSMutableArray *sortedValues = [NSMutableArray array]; for (NSString *key in sortedKeys) [sortedValues addObject: [dict objectForKey: key]]; It is much simpler to use objectsForKeys:notFoundMarker: for that NSDictionary *yourDictionary; NSArray *sortedKeys = [yourDictionary.allKeys sortedArrayUsingDescriptors:@[[NSSortDescriptor sortDescriptorWithKey:@"self" ascending:YES]

Getting NSDictionary keys sorted by their respective values

丶灬走出姿态 提交于 2019-11-26 19:55:19
I have an NSMutableDictionary with integer values, and I'd like to get an array of the keys, sorted ascending by their respective values. For example, with this dictionary: mutableDict = { "A" = 2, "B" = 4, "C" = 3, "D" = 1, } I'd like to end up with the array ["D", "A", "C", "B"] . My real dictionary is much larger than just four items, of course. Hermann Klecker The NSDictionary Method keysSortedByValueUsingComparator: should do the trick. You just need a method returning an NSComparisonResult that compares the object's values. Your Dictionary is NSMutableDictionary * myDict; And your Array

How can I sort an NSArray containing NSDictionaries?

最后都变了- 提交于 2019-11-26 19:07:38
I have an NSArray which contains NSDictionary objects. Each of these NSDictionary objects contains an ORDER key. How can I sort this NSArray based on this key within each of these NSDictionaries ? Here is an example. Imagines you have an array of dictionnaries. Ecah dictionnary have 2 keys "name" & "dateOfBirth". You can sort your array by "name" like this : // // Sort array by name // NSSortDescriptor *Sorter = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES]; [myArray sortUsingDescriptors:[NSArray arrayWithObject:Sorter]]; [Sorter release]; Note that myArray is a NSMutableArray .

ios 学习笔记 4

佐手、 提交于 2019-11-26 18:59:33
1. 常见问题   a,为什么创建的UIButton 点击不了,没有反应?    原因可能是:UIView 在initWithFrame时候没有小了,超出UIWindow 区域。 b, presentModalViewController动画效果    modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;   [self dismissModalViewControllerAnimated:YES]; c, 为什么NSDictionary的键值总是null    初始化数字的时候最好不要直接赋值,要像这样[NSNumber numberWithInt:0 ] d, loadView为什么会不停的执行(循环)    如下:    - ( void )loadView { HelloView *helloView=[[[HelloView alloc] init]autorelease]; [helloView setNeedsDisplay]; [self.view addSubview:helloView]; } 是因为缺少 [super loadView]; 正确的是 - ( void )loadView { [super loadView]; HelloView *helloView=[[[HelloView