Illep
Illep

Reputation: 16859

How to save Values to a NSDictionary

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

Key ------ Value

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

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

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

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

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

Upvotes: 1

Views: 358

Answers (2)

Stephen Darlington
Stephen Darlington

Reputation: 52575

  1. [NSDictionary dictionaryWithObjectsAndKeys:@"value", @"key", @"value", @"key2", nil]
  2. [dict allKeys] or [dict allValues]
  3. 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

Upvotes: 5

Dmitry S.
Dmitry S.

Reputation: 8513

You need to use a NSMutableDictionary instance. There are methods like :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.

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

Upvotes: 2

Related Questions