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
22
votes
4 answers

how to do true deep copy for NSArray and NSDictionary with have nested arrays/dictionary?

Question: Is there a way to use existing objective-c methods to do a full deep copy of a NSDictionary or NSArray, that themselves have nested dictionaries or arrays within them? That is I have read the problem may be when it hits a nested…
Greg
  • 34,042
  • 79
  • 253
  • 454
22
votes
3 answers

NSCoding of NSMutableDictionaries containing custom objects

I was trying to serialize a SearchEntity object(custom object) containing an NSMutableDictionary containing a set of type CategoryEntity(custom object). 1 SearchEntity containing: 1 NSMutableDictionary (parameters) parameters…
RickiG
  • 11,380
  • 13
  • 81
  • 120
21
votes
3 answers

Cocoa's NSDictionary: why are keys copied?

All objects used as keys in NS(Mutable)Dictionaries must support the NSCopying protocol, and those objects are copied when they're used in the dictionary. I frequently want to use heavier weight objects as keys, simply to map one object to another.…
Ben Zotto
  • 70,108
  • 23
  • 141
  • 204
20
votes
6 answers

Sort an NSMutableDictionary

I have an NSMutableDictionary that maps NSString to NSString (although the values are NSStrings, they are really just integers). For example consider the following mappings, "dog" --> "4" "cat" --> "3" "turtle" --> "6" I'd like to end up with the…
CodeGuy
  • 28,427
  • 76
  • 200
  • 317
20
votes
3 answers

How to get the value for each key in dictionary in Objective-C?

I'm maintaining a NSMutableDictionary which holds key and value pair.Now i need to perform some operation for each value in it.How to retrive value from dictionary. // this is NSMutableDIctionary NSMutableDictionary *dictobj = [[NSMutableDictionary…
suse
  • 10,503
  • 23
  • 79
  • 113
19
votes
3 answers

Add values in NSMutableDictionary in iOS with Objective-C

I'm starting objective-c development and I would like to ask the best way to implement a list of keys and values. In Delphi there is the class TDictionary and I use it like this: myDictionary : TDictionary; bool found =…
Miguel E
  • 1,316
  • 2
  • 17
  • 39
19
votes
4 answers

How to clear a dictionary?

I'm currently doing the following to clear out an NSMutableDictionary [myDictionary release]; myDictionary = [[NSMutableDictionary alloc] init]; The release line doesn't actually release any objects in the dictionary. I can still see all of them on…
4thSpace
  • 43,672
  • 97
  • 296
  • 475
19
votes
3 answers

Observing NSMutableDictionary changes

Is it possible to observe (subscribe to) changes to values stored under different keys in an NSMutableDictionary? In my case the keys would already exist when the subscription is initiated, but the values change and I would like to be notified in…
Stig Brautaset
  • 2,602
  • 1
  • 22
  • 39
18
votes
6 answers

[__NSDictionaryI setObject:forKey:]: unrecognized selector sent to instance

I am trying to add "dateTime" to My dictionary as defined follows: Symptom Ranking: { 5111ef19253b4a9150000000 = 1; 5111f029253b4add4e000000 = 1; 5111f036253b4a123d000001 = 1; 5111f045253b4a404f000000 = 1; } NSLog(@"date selected:…
Strong Like Bull
  • 11,155
  • 36
  • 98
  • 169
16
votes
2 answers

How to make a if statement with NSNull in objective-c

I am develop a iPhone application, in which i need to use JSON to receive data from server. In the iPhone side, I convert the data into NSMutableDictionary. However, there is a date type data are null. I use the following sentence to read the…
Fan Wu
  • 237
  • 1
  • 4
  • 11
14
votes
6 answers

NSMutableDictionary: mutating method sent to immutable object

The following code is returning an exception with the following error message "mutating method sent to immutable object" when attempting to removeObjectForKey NSMutableDictionary * storedIpDictionary = (NSMutableDictionary*)[[NSUserDefaults…
Remixed123
  • 1,575
  • 4
  • 21
  • 35
13
votes
4 answers

How to get NSMutableDictionary count in iphone?

I want to get NSMutableDictionary count in iphone. I want to know how many items are in NSMutableDictionry. I tried these code to find out the solution but, not helped me lot. NSLog(@"Count : %d", [mutableDictionary count]); It is always returns…
Gopinath
  • 5,392
  • 21
  • 64
  • 97
13
votes
2 answers

how to insert a (BOOL *) into NSMutableDictionary

I am trying to save a boolean database value into a map as follows - [recentTags setValue:[NSNumber numberWithBool:[aMessage isSet]] forKey:[aMessage tagName]]; It gives me an error saying "Incompatible pointer to integer conversion sending BOOL *…
Suchi
  • 9,989
  • 23
  • 68
  • 112
13
votes
4 answers

NSMapTable and NSMutableDictionary differences

Is an NSMapTable the same as an NSMutableDictionary except for allowing keys to be pointers? Does it differ in memory management?
123hal321
  • 2,080
  • 4
  • 24
  • 25
12
votes
3 answers

How to remove elements in NSMutableArray or NSMutableDictionary during enumeration?

I am using block based enumeration similar to the following code: [[[rows objectForKey:self.company.coaTypeCode] objectForKey:statementType] enumerateObjectsWithOptions:NSEnumerationConcurrent usingBlock:^(id coaItem,…
1
2
3
87 88