如何将值保存到NSDictionary

时间:2022-01-26 12:21:12

I need to know;

我需要知道;

1.) how to save values to a NSDictionary, and in Key-Value pairs. I have the following keys and values to be saved to a single NSDictionary

1.)如何将值保存到NSDictionary和键值对中。我有以下键和值保存到单个NSDictionary

Key ------ Value

核心价值

"1" ------- "One"

“1”-------“一个”

"2" -------"Two"

"3" -------- "Three" etc..

“3”--------“三”等

2.) Later i need to save the above NSDictionary to a NSArray. Is this possible ?

2.)后来我需要将上面的NSDictionary保存到NSArray中。这可能吗 ?

3.) Is there a way to Order the above NSDictionary in Assending order. How can i do this

3.)有没有办法在资产订购中订购上述NSDictionary。我怎样才能做到这一点

2 个解决方案

#1


4  

  1. [NSDictionary dictionaryWithObjectsAndKeys:@"value", @"key", @"value", @"key2", nil]
  2. [NSDictionary dictionaryWithObjectsAndKeys:@“value”,@“key”,@“value”,@“key2”,nil]

  3. [dict allKeys] or [dict allValues]
  4. [dict allKeys]或[dict allValues]

  5. A dictionary is a map from a value to a key. Ordering does not make sense. If you get the keys from the dictionary, you can sort the values in that as you would an ordinary NSArray
  6. 字典是从值到键的映射。订购没有意义。如果从字典中获取密钥,则可以像普通的NSArray一样对其中的值进行排序

#2


2  

You need to use a NSMutableDictionary instance. There are methods like :setObject:forKey

您需要使用NSMutableDictionary实例。有一些方法,如:setObject:forKey

https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/nsmutabledictionary_Class/Reference/Reference.html

After you add dictionary values to an NSMutableArray instance, you can use the following method to sort them.

将字典值添加到NSMutableArray实例后,可以使用以下方法对它们进行排序。

[arrayInstance sortUsingSelector:@selector(localizedCaseInsensitiveCompare:)];

#1


4  

  1. [NSDictionary dictionaryWithObjectsAndKeys:@"value", @"key", @"value", @"key2", nil]
  2. [NSDictionary dictionaryWithObjectsAndKeys:@“value”,@“key”,@“value”,@“key2”,nil]

  3. [dict allKeys] or [dict allValues]
  4. [dict allKeys]或[dict allValues]

  5. A dictionary is a map from a value to a key. Ordering does not make sense. If you get the keys from the dictionary, you can sort the values in that as you would an ordinary NSArray
  6. 字典是从值到键的映射。订购没有意义。如果从字典中获取密钥,则可以像普通的NSArray一样对其中的值进行排序

#2


2  

You need to use a NSMutableDictionary instance. There are methods like :setObject:forKey

您需要使用NSMutableDictionary实例。有一些方法,如:setObject:forKey

https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/nsmutabledictionary_Class/Reference/Reference.html

After you add dictionary values to an NSMutableArray instance, you can use the following method to sort them.

将字典值添加到NSMutableArray实例后,可以使用以下方法对它们进行排序。

[arrayInstance sortUsingSelector:@selector(localizedCaseInsensitiveCompare:)];