NSMutableDictionary addEntriesFromDictionary
- (void)addEntriesFromDictionary:(NSDictionary *)otherDictionary
Parameters of [NSMutableDictionary addEntriesFromDictionary]
otherDictionary
The dictionary from which to add entries
Discussion of [NSMutableDictionary addEntriesFromDictionary]
Each value object from otherDictionary is sent a retain message before being added to the receiving dictionary. In contrast, each key object is copied (using copyWithZone:—keys must conform to the NSCopying protocol), and the copy is added to the receiving dictionary.
If both dictionaries contain the same key, the receiving dictionary’s previous value object for that key is sent a release message, and the new value object takes its place.
NSMutableDictionary addEntriesFromDictionary example.
make a deep copy of the original dictionary before adding it to the new dictionary:
NSDictionary *deepCopy = [[NSDictionary alloc] initWithDictionary: original copyItems: YES];
if (deepCopy) {
[destination addEntriesFromDictionary: deepCopy];
[deepCopy release];
}
NSDictionary *deepCopy = [[NSDictionary alloc] initWithDictionary: original copyItems: YES];
if (deepCopy) {
[destination addEntriesFromDictionary: deepCopy];
[deepCopy release];
}
Example of [NSMutableDictionary addEntriesFromDictionary].
// Make an immutable dictionary by combining 2 dictionaries (new entries are added, old entries are updated)
+ (NSDictionary *)dictionaryFromDictionary:(NSDictionary *)original WithAddedEntries:(NSDictionary *)entriesToAdd
{
NSMutableDictionary *final = [NSMutableDictionary dictionaryWithDictionary:original];
[final addEntriesFromDictionary:entriesToAdd];
// Done
return [NSDictionary dictionaryWithDictionary:final];
}
+ (NSDictionary *)dictionaryFromDictionary:(NSDictionary *)original WithAddedEntries:(NSDictionary *)entriesToAdd
{
NSMutableDictionary *final = [NSMutableDictionary dictionaryWithDictionary:original];
[final addEntriesFromDictionary:entriesToAdd];
// Done
return [NSDictionary dictionaryWithDictionary:final];
}
NSMutableDictionary addEntriesFromDictionary example.
- (NSDictionary *) plist{
NSMutableDictionary *resultDict = [[[NSMutableDictionary alloc] init] autorelease];
if ([self hasChildren]){
NSMutableDictionary *child = [[[NSMutableDictionary alloc]init] autorelease];
for (OSXMLElement *element in _children){
[child addEntriesFromDictionary:[element plist]]; }
[resultDict setValue:child forKey:_elementName];
}
else {
[resultDict setValue:_elementText forKey:_elementName];
}
return resultDict;
}
NSMutableDictionary *resultDict = [[[NSMutableDictionary alloc] init] autorelease];
if ([self hasChildren]){
NSMutableDictionary *child = [[[NSMutableDictionary alloc]init] autorelease];
for (OSXMLElement *element in _children){
[child addEntriesFromDictionary:[element plist]]; }
[resultDict setValue:child forKey:_elementName];
}
else {
[resultDict setValue:_elementText forKey:_elementName];
}
return resultDict;
}