nsdictionary

How can I convert NSDictionary to NSData and vice versa?

为君一笑 提交于 2019-11-26 14:58:57
问题 I am sending NSString and UIImage using bluetooth. I decided to store both in a NSDictionary and then convert the dictionary to NSData . My question is how to convert NSDictionary to NSData and visa versa? 回答1: NSDictionary -> NSData: NSData *myData = [NSKeyedArchiver archivedDataWithRootObject:myDictionary]; NSData -> NSDictionary: NSDictionary *myDictionary = (NSDictionary*) [NSKeyedUnarchiver unarchiveObjectWithData:myData]; 回答2: NSDictionary -> NSData: NSMutableData *data = [

How can i get Original order of NSDictionary/NSMutableDictionary?

你说的曾经没有我的故事 提交于 2019-11-26 14:45:49
问题 i have created NSMutableDictionary with 10 keys.Now i want to access NSMutableDictionary keys in a same order as it was added to NSMutableDictionary (using SetValue:* forKey:* ); How can i achieve that ? 回答1: If you absolutely must use a dictionary container, you have to use a key that is sortable by the order in which you add key-value pairs. Thus, when creating your dictionary, you use a key that is an auto-incrementing integer or similar. You can then sort on the (integer) keys and

Replace occurrences of NSNull in nested NSDictionary

我的未来我决定 提交于 2019-11-26 14:37:11
问题 This question is similar to this question, however this method only works on the root level of the dictionary. I'm looking to replace any occurrence of NSNull values with an empty string, so that I can save the full dictionary to a plist file (if i add it with the NSNull's the file won't write). My dictionary, however, has nested dictionaries inside it. Like this: "dictKeyName" = { innerStrKeyName = "This is a string in a dictionary"; innerNullKeyName = "<null>"; innerDictKeyName = {

Best way to sort an NSArray of NSDictionary objects?

点点圈 提交于 2019-11-26 14:16:19
I'm struggling with trying to sort an array of dictionaries. My dictionaries have a couple of values of interest, price, popularity etc. Any suggestions? Warrior Use NSSortDescriptor like this.. NSSortDescriptor * descriptor = [[NSSortDescriptor alloc] initWithKey:@"interest" ascending:YES]; stories = [stories sortedArrayUsingDescriptors:@[descriptor]]; recent = [stories copy]; stories is the array you want to sort. recent is another mutable array which has sorted dictionary values. Change the @"interest" with the key value on which you have to sort. All the best superarts.org [array

How to append elements into a dictionary in Swift?

房东的猫 提交于 2019-11-26 12:55:38
问题 I have a simple Dictionary which is defined like: var dict : NSDictionary = [ 1 : \"abc\", 2 : \"cde\"] Now I want to add an element into this dictionary: 3 : \"efg\" How can I append 3 : \"efg\" into this existing dictionary? 回答1: You're using NSDictionary . Unless you explicitly need it to be that type for some reason, I recommend using a Swift dictionary. You can pass a Swift dictionary to any function expecting NSDictionary without any extra work, because Dictionary<> and NSDictionary

What is an NSCFDictionary?

偶尔善良 提交于 2019-11-26 12:47:52
问题 I\'m getting an NSCFDictionary returned to me and I can\'t figure out how to use it. I know it\'s of type NSCFDictionary because I printed the class and it came out as __NCSFDictionary. I can\'t figure out how to do anything with it. I\'m just trying to hold onto it for now but can\'t even get that to work: NSDictionary *dict = [[NSURLCredentialStorage sharedCredentialStorage] allCredentials]; for(NSURLProtectionSpace key in [dict keyEnumerator]) { NSCFDictionary *value = [dict objectForKey

Swift - Stored values order is completely changed in Dictionary

徘徊边缘 提交于 2019-11-26 12:31:06
I tried to display datas which is in Dictionary format. Below, three attempts are there. First attempt, output order is completely changed. Second attempt, output order is same as input. But, in third attempt, I declared variable as NSDictionary. Exact output I received. Why this changes in Dictionary? Kindly guide me. I searched for Swift's Dictionary tag. But I couldn't found out. //First Attempt var dict : Dictionary = ["name1" : "Loy", "name2" : "Roy"] println(dict) //output: [name2: Roy, name1: Loy] //Second Attempt var dict : Dictionary = ["name2" : "Loy", "name1" : "Roy"] println(dict)

writing NSDictionary to plist in my app bundle

拥有回忆 提交于 2019-11-26 12:17:51
问题 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: %@

is it possible to save NSMutableArray or NSDictionary data as file in iOS?

大城市里の小女人 提交于 2019-11-26 12:09:27
问题 I want to save an NSMutableArray or NSDictionary as a permanent file. Then, I would like to reopen the file later. Is this possible? If so, how can I do it? 回答1: To write in file NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSString *filePath = [documentsDirectory stringByAppendingPathComponent:FILE_NAME]; [myArray writeToFile:filePath atomically:YES]; To read from file NSArray *paths

Best practice? - Array/Dictionary as a Core Data Entity Attribute [closed]

一世执手 提交于 2019-11-26 11:58:36
I am new to Core Data. I have noticed that collection types are not available as attribute types and would like to know what the most efficient way is of storing array/dictionary type data as an attribute (e.g. the elements that make up an address like street, city, etc. does not require a separate entity and is more conveniently stored as a dictionary/array than separate attributes/fields). Thank you. There is no "native" array or dictionary type in Core Data. You can store an NSArray or an NSDictionary as a transformable attribute. This will use the NSCoding to serialize the array or