nsdictionary

thread safe nsdictionary

一曲冷凌霜 提交于 2020-01-06 15:08:30
问题 I have an nsdictionary that gets read from A LOT and at some points concurrently written to A LOT. To help make it thread safe when iterating it, I copy it, iterate the copy, and add to the original. Sometimes, another thread is copying it while it is being added to. Is there a better way to make it thread safe? 回答1: If you have changes you want to make to an NSMutableDictionary and need the operations to be done in a thread-safe manner, the simplest way is to wrap all accesses on that object

Sharing immutable NSStrings

一个人想着一个人 提交于 2020-01-06 12:36:56
问题 I have a list of about 10k dictionaries, each dictionary contains about 50 keys. Keys are more or less the same for all dictionaries. The data is loaded using NSDictionary.alloc.initWithContentsOfFile. It seems that the key objects are reused between different dictionaries, and thus instead of having about 500k strings in memory, I have only one string per unique key, thus just a couple hundreds of them. So I wonder if it is an expected behavior of initWithContentsOfFile method and I can rely

Sharing immutable NSStrings

别来无恙 提交于 2020-01-06 12:36:16
问题 I have a list of about 10k dictionaries, each dictionary contains about 50 keys. Keys are more or less the same for all dictionaries. The data is loaded using NSDictionary.alloc.initWithContentsOfFile. It seems that the key objects are reused between different dictionaries, and thus instead of having about 500k strings in memory, I have only one string per unique key, thus just a couple hundreds of them. So I wonder if it is an expected behavior of initWithContentsOfFile method and I can rely

NSDictionary dictionaryWithContentsOfFile doesnt load the data in order

╄→尐↘猪︶ㄣ 提交于 2020-01-06 08:39:14
问题 I am trying to load some data from a plist into an array NSDictionary* mydictionary = [ NSDictionary dictionaryWithContentsOfFile:[ [ [ NSBundle mainBundle] bundlePath ] stringByAppendingPathComponent:@"MyLevels.plist" ] ]; NSMutableArray *panelNames1; panelNames1 = [[NSMutableArray alloc] init]; for (id theKey in mydictionary) { [panelNames1 addObject:[mydictionary objectForKey:theKey]]; } But it does not seem to output in the same order as the MyLevels.plist into the array. MyLevels.plist

Bi-directional dictionary in objective-c

南楼画角 提交于 2020-01-06 06:37:53
问题 Suppose I have key-value pairs having one to one mapping. For each key I have unique value. Then can I make a bi-directional dictionary or something similar to it which can give me value from key and vice versa? P.S. - I know that I can use [NSDictionary allKeysForObject: (nonnull id)] which returns an NSArray of all keys having that value. I was just wondering if there is something like bi-directional dictionary then it will be useful. If there is something like that then please provide

Bi-directional dictionary in objective-c

醉酒当歌 提交于 2020-01-06 06:37:19
问题 Suppose I have key-value pairs having one to one mapping. For each key I have unique value. Then can I make a bi-directional dictionary or something similar to it which can give me value from key and vice versa? P.S. - I know that I can use [NSDictionary allKeysForObject: (nonnull id)] which returns an NSArray of all keys having that value. I was just wondering if there is something like bi-directional dictionary then it will be useful. If there is something like that then please provide

iPhone : JSON message not creating correctly

假装没事ソ 提交于 2020-01-06 05:09:25
问题 I used the following code : [dict setObject:[NSString stringWithFormat:@"%d",1] forKey:@"res_id"]; //[dict setObject:categoryId forKey:@"category"]; NSArray * values = [cartDict allValues]; NSString *request1 = [dict JSONRepresentation]; NSLog(request1); NSDictionary *req = [NSDictionary dictionaryWithObject:dict forKey:@""]; //convert object to data NSData *jsonData = [NSData dataWithBytes:[request1 UTF8String] length:[request1 length]]; //print out the data contents NSString *json1 = [

OC文件操作2

纵然是瞬间 提交于 2020-01-05 23:54:37
1、对文件本身的操作 NSManager 2、对文件内容的操作 NSHandle 文件句柄 1 NSFileHandle * fh = [NSFileHandle fileHandleForReadingAtPath:@""]; //以只读方式打开文件生成文件句柄 内存:内部存储器;硬盘:外部存储设备。 从硬盘到内存(从文件到内存)叫做读,从内存到文件(硬盘)叫做写。 1 //读取文件内容的两种方式 2 NSData * data = [fh readDataOfLength:3]; 3 //继续上面3个字节后,继续读取5个字节 4 data = [fh readDataOfLength:5]; 如果文件内容不是特别的多,可以用下面的方法直接读取全部内容 1 //如果文件内容不是特别的多,可以用下面的方法直接读取全部内容 2 data = [fh readDataToEndOfFile]; 3 NSString * str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; 除了上面读的方法,还有写的方法。 1 NSFileHandle * fh = [NSFileHandle fileHandleForWritingAtPath:@""]; //以只写方式打开文件生成句柄 2 [fh

How to access the next key in a Swift dictionary?

久未见 提交于 2020-01-05 22:57:12
问题 I have this code for (k, v) in myDict { println(k) } How do I access the next key in the dictionary (e.g. myDict[k + 1])? Thanks in advance! 回答1: There is no such thing as "the next key"; dictionaries have no order. Since, however, you are iterating through the dictionary... for (k, v) in myDict { println(k) } I'm going to assume that what you mean is: how can I know, on this iteration, what k would be on the next iteration? A simple solution would be to coerce the dictionary to an array (of

iOS - Add NSDictinary key values into an NSArray sequencialy

旧街凉风 提交于 2020-01-05 21:12:21
问题 I have a NSDictionary and a NSArray : self.dataDic = [[[NSDictionary alloc] init] autorelease]; self.dataDicKey = [[[NSArray alloc] init] autorelease]; These are the key and value of my NSDictionary : self.dataDic = @{@"Ring Cloud" :@[@"Contact Cloud", @"Image Cloud", @"Data Cloud"], @"Ring Tools" :@[@"Sticker Market", @"Live Poll", @"Mail"], @"Ring Apps" :@[@"Studio", @"Player"]}; Now what I want is to insert all the key value of my self.dataDic into self.dataDicKey sequentially. That means