Questions tagged [nsmutabledictionary]

NSMutableDictionary is the mutable version of NSDictionary.

Like NSDictionary (), it is a subclass of NSObject (), that allows you to have a collection of objects, but instead of having them stored in an array retrievable by their index, they are stored as a key-value pair. Each object stored in an NSDictionary can be retrieved by a unique key assigned to it. Keys have to be unique and can be any object as long as they adopt the NSCopying protocol – they are usually an instance of NSString ().

Neither a key nor the object paired with it can be nil, however, you may use NSNull for storing null objects.

If two objects are stored with the same key, the first one is released, and the last one is stored.

When using key-value coding, the key must be a string. When using key-object coding, the key is copied using copyWithZone: and it must conform to the NSCopying protocol. This object is retained, rather than copy.

NSMutableDictionary gives you the ability to modify this set of key-object paired collection without having to empty it and refill it again as a whole set. The following methods are for this purpose:

Adding Entries to a Mutable Dictionary

– setObject:forKey: 
– setValue:forKey: 
– addEntriesFromDictionary: 
– setDictionary:

Removing Entries From a Mutable Dictionary

– removeObjectForKey: 
– removeAllObjects 
– removeObjectsForKeys:

Resources:

1306 questions
284
votes
7 answers

for each loop in Objective-C for accessing NSMutable dictionary

I am finding some difficulty in accessing mutable dictionary keys and values in Objective-C. Suppose I have this: NSMutableDictionary *xyz=[[NSMutableDictionary alloc] init]; I can set keys and values. Now, I just want to access each key and value,…
sagarkothari
  • 24,520
  • 50
  • 165
  • 235
156
votes
4 answers

Where's the difference between setObject:forKey: and setValue:forKey: in NSMutableDictionary?

When looking at the documentation, I hardly see any big difference. Both "value" and "object" are of type id, so can be any object. Key is once a string, and in the other case an id. One of them seems to retain the object, and the other don't. What…
HelloMoon
95
votes
6 answers

looping through an NSMutableDictionary

How do I loop through all objects in a NSMutableDictionary regardless of the keys?
Rupert
  • 953
  • 1
  • 6
  • 5
67
votes
3 answers

How can we store into an NSDictionary? What is the difference between NSDictionary and NSMutableDictionary?

I am developing an application in which I want to use an NSDictionary. Can anyone please send me a sample code explaining the procedure how to use an NSDictionary to store Data with a perfect example?
Pradeep Reddy Kypa
  • 3,992
  • 7
  • 57
  • 75
57
votes
5 answers

Is there a literal syntax for mutable collections?

I know I can create an NSArray with @[@"foo", @"bar"] or an NSDictionary with @{@0 : @"foo", @1 : @"bar"}. Is there a literal syntax for creating an NSMutableArray or an NSMutableDictionary?
45
votes
4 answers

How can I convert an NSDictionary to an NSMutableDictionary?

I have an existing NSDictionary that has: { "charts_count" = 2; "created_at" = "2010-04-12T16:37:32Z"; exchange = NASDAQ; "followers_count" = 259; id = 8404; industry = ""; "messages_count" = 1436; ric =…
Sheehan Alam
  • 60,111
  • 124
  • 355
  • 556
45
votes
3 answers

How can I use an integer value as 'key' to set value in NSMutableDictionary?

How can I use an integer value as 'key' to set a float value in NSMutableDictionary ?
Ratan
  • 1,747
  • 2
  • 18
  • 27
44
votes
6 answers

NSMutableDictionary thread safety

I have a question on thread safety while using NSMutableDictionary. The main thread is reading data from NSMutableDictionary where: key is NSString value is UIImage An asynchronous thread is writing data to above dictionary (using…
Raj
  • 636
  • 1
  • 8
  • 12
43
votes
3 answers

Convert NSDictionary to Swift Dictionary

Now I know that when swift compiles it just makes a NSDictionary, but the NSDictionary and Swift dictionaries have different syntax. Is there a way (through a loop or something) to convert a NSDictionary to a swift dictionary of the same type for…
cclloyd
  • 8,171
  • 16
  • 57
  • 104
34
votes
5 answers

How to sort an array which contains Dictionaries?

I have an array which contains dictionaries in it; so how can I sort the array according to dictionary key values?
Ankit Vyas
  • 7,507
  • 13
  • 56
  • 89
33
votes
4 answers

How to get the key for a given object from an NSMutableDictionary?

I have an object which is in an big NSMutableDictionary, and need to find out which key this has. So I want to look up that "table" from both columns. Not only with keys, but also with objects (to get keys). Is that possible?
dontWatchMyProfile
  • 45,440
  • 50
  • 177
  • 260
30
votes
4 answers

How to insert a new key with value in dictionary

I want to insert a key with a corresponding value in an existing dictionary... I am able to set values for existing keys in the dictionary, but I am not able to add a new key value... Any help would be appreciated.
Linux world
  • 3,750
  • 11
  • 44
  • 59
30
votes
2 answers

How to create an NSDictionary in Objective-C?

I want to create an NSDictionary like this type: "zones": { { "zoneId": "1", "locations": { { "locId": "1", "locZoneId": "1", "locLatitude":…
vky
  • 475
  • 1
  • 6
  • 14
28
votes
4 answers

NSMutable Dictionary adding objects

Is there a more efficient way to add objects to an NSMutable Dictionary than simple iteration? Example: // Create the dictionary NSMutableDictionary *myMutableDictionary = [NSMutableDictionary dictionary]; // Add the…
djt9000
  • 367
  • 1
  • 5
  • 10
24
votes
3 answers

How to addObject of NSMutableDictionary to NSMutableArray in a loop

I'm having difficulty to add a data structure to an array to create an array of dictionaries from a loop. I just knew that addObject in NSMutableArray only add pointer to it. I would like to know how to achieve my goal. Here's my…
Ber Biji II
  • 273
  • 1
  • 2
  • 6
1
2 3
87 88