Showing posts with label NSMutableDictionary. Show all posts
Showing posts with label NSMutableDictionary. Show all posts

Wednesday, June 19, 2013

NSMutableDictionary setValue forKey example in Objective C (iOS).


NSMutableDictionary setValue forKey

Adds a given key-value pair to the dictionary.

- (void)setValue:(id)value forKey:(NSString *)key

Parameters of [NSMutableDictionary setValue forKey]
value
The value for key.
key
The key for value. Note that when using key-value coding, the key must be a string (see “Key-Value Coding Fundamentals”).

Discussion of [NSMutableDictionary setValue forKey]
This method adds value and key to the dictionary using setObject:forKey:, unless value is nil in which case the method instead attempts to remove key using removeObjectForKey:.

NSMutableDictionary setValue forKey example.
NSMutableDictionary *parameters = [[NSMutableDictionary alloc] init];
[parameters setValue:[NSNumber numberWithBool:news] forKey:@"news"];
[parameters setValue:[NSNumber numberWithBool:mails] forKey:@"mails"];

End of NSMutableDictionary setValue forKey example article.

NSMutableDictionary setValue example in Objective C (iOS).


NSMutableDictionary setValue

Adds a given key-value pair to the dictionary.

- (void)setValue:(id)value forKey:(NSString *)key

Parameters of [NSMutableDictionary setValue]
value
The value for key.
key
The key for value. Note that when using key-value coding, the key must be a string (see “Key-Value Coding Fundamentals”).

Discussion of [NSMutableDictionary setValue]
This method adds value and key to the dictionary using setObject:forKey:, unless value is nil in which case the method instead attempts to remove key using removeObjectForKey:.

NSMutableDictionary setValue example.
NSMutableDictionary *parameters = [[NSMutableDictionary alloc] init];
[parameters setValue:[NSNumber numberWithBool:news] forKey:@"news"];
[parameters setValue:[NSNumber numberWithBool:mails] forKey:@"mails"];

End of NSMutableDictionary setValue example article.

NSMutableDictionary setObject forKey example in Objective C (iOS).


NSMutableDictionary setObject forKey

Adds a given key-value pair to the dictionary.

- (void)setObject:(id)anObject forKey:(id < NSCopying >)aKey

Parameters of [NSMutableDictionary setObject forKey]
anObject
The value for aKey. A strong reference to the object is maintained by the dictionary. Raises an NSInvalidArgumentException if anObject is nil. If you need to represent a nil value in the dictionary, use NSNull.
aKey
The key for value. The key is copied (using copyWithZone:; keys must conform to the NSCopying protocol). Raises an NSInvalidArgumentException if aKey is nil. If aKey already exists in the dictionary anObject takes its place.

NSMutableDictionary setObject forKey example.
NSMutableDictionary* dict = [[NSMutableDictionary alloc] initWithDictionary:delegate.sharedData.dictFaves];
[dict setObject:@"test" forKey:@"4"];
delegate.sharedData.dictFaves = dict;
[dict release];

Example of [NSMutableDictionary setObject forKey].
- (BOOL)addStudent:(Student *)newStudent
{
    NSLog(@"adding new student");
    if (studentStore == nil) {
        studentStore = [[NSMutableDictionary alloc] init];
    }
    [studentStore setObject:newStudent forKey:newStudent.adminNo];
    return YES;
}

NSMutableDictionary setObject forKey example.
//NSMutableDictionary myDictionary = [[NSMutableDictionary alloc] init];
NSMutableDictionary *myDictionary = [[NSMutableDictionary alloc] init];

NSNumber *value = [myDictionary objectForKey:myWord];

if (value)
{
    NSNumber *nextValue = [NSNumber numberWithInt:[value intValue] + 1];
    [myDictionary setObject:nextValue  forKey:myWord];
}
else
{
    [myDictionary setObject:[NSNumber numberWithInt:1] forKey:myWord]
}

End of NSMutableDictionary setObject forKey example article.

NSMutableDictionary setObject example in Objective C (iOS).


NSMutableDictionary setObject

Adds a given key-value pair to the dictionary.

- (void)setObject:(id)anObject forKey:(id < NSCopying >)aKey

Parameters of [NSMutableDictionary setObject]
anObject
The value for aKey. A strong reference to the object is maintained by the dictionary. Raises an NSInvalidArgumentException if anObject is nil. If you need to represent a nil value in the dictionary, use NSNull.
aKey
The key for value. The key is copied (using copyWithZone:; keys must conform to the NSCopying protocol). Raises an NSInvalidArgumentException if aKey is nil. If aKey already exists in the dictionary anObject takes its place.

NSMutableDictionary setObject example.
NSMutableDictionary* dict = [[NSMutableDictionary alloc] initWithDictionary:delegate.sharedData.dictFaves];
[dict setObject:@"test" forKey:@"4"];
delegate.sharedData.dictFaves = dict;
[dict release];

Example of [NSMutableDictionary setObject].
- (BOOL)addStudent:(Student *)newStudent
{
    NSLog(@"adding new student");
    if (studentStore == nil) {
        studentStore = [[NSMutableDictionary alloc] init];
    }
    [studentStore setObject:newStudent forKey:newStudent.adminNo];
    return YES;
}

NSMutableDictionary setObject example.
//NSMutableDictionary myDictionary = [[NSMutableDictionary alloc] init];
NSMutableDictionary *myDictionary = [[NSMutableDictionary alloc] init];

NSNumber *value = [myDictionary objectForKey:myWord];

if (value)
{
    NSNumber *nextValue = [NSNumber numberWithInt:[value intValue] + 1];
    [myDictionary setObject:nextValue  forKey:myWord];
}
else
{
    [myDictionary setObject:[NSNumber numberWithInt:1] forKey:myWord]
}

End of NSMutableDictionary setObject example article.

NSMutableDictionary setDictionary example in Objective C (iOS).


NSMutableDictionary setDictionary

Sets the contents of the receiving dictionary to entries in a given dictionary.

- (void)setDictionary:(NSDictionary *)otherDictionary

Parameters
otherDictionary
A dictionary containing the new entries.

Discussion of [NSMutableDictionary setDictionary]
All entries are removed from the receiving dictionary (with removeAllObjects), then each entry from otherDictionary added into the receiving dictionary.

NSMutableDictionary setDictionary example.
If you want to change the contents of the dictionary, you could just replace it with the new dictionary. In one line:

[dictionaryIvar setDictionary:[NSMutableDictionary dictionary]];

That has the same effect of doing a [dictionaryIvar removeAllObjects] (which also sends a release to all objects contained in it), followed by adding the objects from the new dictionary (in your case, empty).

Also, by doing this you don't have to worry about your ivar's ownership and memory management issues if all you need to do is 'reset' the dictionary...

Example of [NSMutableDictionary setDictionary].
[array replaceObjectAtIndex:i withObject:targetDict];
or

[tempDict setDictionary:targetDict]

NSMutableDictionary setDictionary example.
NSArray *columns = [fileBrowser tableColumns];
    int index, count = [columns count];
    for (index = 0; index < count; index++) {
NSTableColumn *column = [columns objectAtIndex:index];
[[column dataCell] setWraps:YES];
    }
.
.
NSMutableDictionary *rec = [[NSMutableDictionary alloc] init];
[rec setDictionary:[self createDicFromFile:fileName atPath:dirName withImageSize:size]];
if([rec count] == 0) continue;
[imageRecord addObject:rec];
[rec release];

End of NSMutableDictionary setDictionary example article.

NSMutableDictionary removeObjectsForKeys example in Objective C (iOS).


NSMutableDictionary removeObjectsForKeys

Removes from the dictionary entries specified by elements in a given array.

- (void)removeObjectsForKeys:(NSArray *)keyArray

Parameters
keyArray
An array of objects specifying the keys to remove.

Discussion of [NSMutableDictionary removeObjectsForKeys]
If a key in keyArray does not exist, the entry is ignored.

NSMutableDictionary removeObjectsForKeys example.
NSMutableArray *keysToDelete = [NSMutableArray array];

for (id obj in [yourDictionary keyEnumerator]) {
    //do stuff with obj
    if (shouldBeDeleted(obj)) {
        [keysToDelete addObject:obj];
    }
}

[yourDictionary removeObjectsForKeys:keysToDelete];

Example of [NSMutableDictionary removeObjectsForKeys].
NSMutableDictionary* d = [NSMutableDictionary dictionaryWithDictionary: bigDict];
[d removeObjectsForKeys: [NSArray arrayWithObjects: @"address", @"age", nil]];

NSMutableDictionary removeObjectsForKeys example.
NSSet *keys = [lastMsgs keysOfEntriesPassingTest:^BOOL(id key, id obj, BOOL *stop) {
    return [obj[@"type"] intValue] == 1;
}];
NSMutableDictionary *dict = [lastMsgs mutableCopy];
[dict removeObjectsForKeys:[keys allObjects]];

End of NSMutableDictionary removeObjectsForKeys example article.

NSMutableDictionary removeObjectForKey example in Objective C (iOS).


NSMutableDictionary removeObjectForKey

Removes a given key and its associated value from the dictionary.

- (void)removeObjectForKey:(id)aKey

Parameters
aKey
The key to remove.

Discussion of [NSMutableDictionary removeObjectForKey]
Does nothing if aKey does not exist.

For example, assume you have an archived dictionary that records the call letters and associated frequencies of radio stations. To remove an entry for a defunct station, you could write code similar to the following:

NSMutableDictionary removeObjectForKey example.
NSMutableDictionary *stations = nil;

stations = [[NSMutableDictionary alloc]
        initWithContentsOfFile: pathToArchive];
[stations removeObjectForKey:@"KIKT"];

Example of [NSMutableDictionary removeObjectForKey].
First you need to use a NSMutableDictionnary and put this code :

[countriesToLiveInDict removeObjectForKey:@"Countries"];
[countriesToLiveInDict setObject:sortedArray forKey:@"Countries"];

NSMutableDictionary removeObjectForKey example.
[dictionary removeObjectForKey:viewerKeys[indexPath.row]];  // remove array from dictionary
[viewerKeys removeObjectAtIndex:indexPath.row]; // remove key from array of dictionary keys

End of NSMutableDictionary removeObjectForKey example article.

NSMutableDictionary removeAllObjects example in Objective C (iOS).


NSMutableDictionary removeAllObjects

Empties the dictionary of its entries.

- (void)removeAllObjects

Discussion of [NSMutableDictionary removeAllObjects]
Each key and corresponding value object is sent a release message.

NSMutableDictionary removeAllObjects example.
[myDictionary removeAllObjects];
[myDictionary release];
myDictionary = [[NSMutableDictionary alloc] init];

Example of [NSMutableDictionary removeAllObjects].
NSMutableDictionary *aLocation=[[NSMutableDictionary alloc] init];
[aLocation setObject:@"26.8465108" forKey:@"lat"];
[aLocation setObject:@"80.9466832" forKey:@"long"];
[aLocation removeAllObjects]

End of NSMutableDictionary removeAllObjects example article.

NSMutableDictionary initWithCapacity example in Objective C (iOS).


NSMutableDictionary initWithCapacity

Initializes a newly allocated mutable dictionary, allocating enough memory to hold numItems entries.

- (id)initWithCapacity:(NSUInteger)numItems

Parameters
numItems
The initial capacity of the initialized dictionary.

Return Value
An initialized mutable dictionary, which might be different than the original receiver.

Discussion of [NSMutableDictionary initWithCapacity]
Mutable dictionaries allocate additional memory as needed, so numItems simply establishes the object’s initial capacity.

NSMutableDictionary initWithCapacity example.
- ( id )init
{
    if( ( self = [ super init ] ) )
    {
        NSMutableDictionary * tempDict = [ [ NSMutableDictionary alloc ] initWithCapacity: 0 ];
        self.baseViewDictionary        = tempDict;

        [ self.baseViewDictionary setObject: @"test" forKey: [ NSNumber numberWithInteger: 0 ] ];
        [ tempDict release ];

        NSLog( @"%@", self.baseViewDictionary );
    }

    return self;
}

Example of [NSMutableDictionary initWithCapacity].
#import <Foundation/Foundation.h>

// Model.h
@interface Model : NSObject {
    NSMutableDictionary *piers;
}
@property (nonatomic,retain) NSMutableDictionary *piers;

-(void) createModel;

@end

// Model.m
@implementation Model
@synthesize piers;

-(id) init {
    if (self = [super init]) {
        self.piers = [[NSMutableDictionary alloc] initWithCapacity:2];
        [self createModel];
    }
    return self;
}

-(void) createModel {
    [piers setObject:@"happy" forKey:@"foobar"]; 
}
@end

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

    // insert code here...
    Model *model = [[Model alloc] init];

    NSLog(@"Model: %@", [model.piers objectForKey:@"foobar"]);

    [pool drain];
    return 0;
}

NSMutableDictionary initWithCapacity example.
- (id)init
{
    if ((self = [super init]) == nil) { return nil; }

    bar = [[NSMutableArray alloc] initWithCapacity:0];

    return self;
}

- (void)dealloc
{
    [bar release];

    [super dealloc];
}

- (void)YourMethod
{
    NSMutableDictionary *foo = [[NSMutableDictionary alloc] initWithCapacity:0];
    [foo setObject:[NSNull null] forKey:@"yay"];
    [bar addObject:foo];
    [foo release];
}

End of NSMutableDictionary initWithCapacity example article.

NSMutableDictionary dictionaryWithCapacity example in Objective C (iOS).


NSMutableDictionary dictionaryWithCapacity

Creates and returns a mutable dictionary, initially giving it enough allocated memory to hold a given number of entries.

+ (id)dictionaryWithCapacity:(NSUInteger)numItems

Parameters
numItems
The initial capacity of the new dictionary.

Return Value
A new mutable dictionary with enough allocated memory to hold numItems entries.

Discussion of [NSMutableDictionary dictionaryWithCapacity]
Mutable dictionaries allocate additional memory as needed, so numItems simply establishes the object’s initial capacity.

NSMutableDictionary dictionaryWithCapacity example.
To set an array as a key for another array as an object. You can do this with the following appraoch.

NSMutableDictionary *myDictionary = [NSMutableDictionary dictionaryWithCapacity:1];

NSArray *valueArray = [NSArray arrayWithObjects:@"v1", @"v2", nil];
NSArray *keyArray = [NSArray arrayWithObjects:@"k1",@"k2", nil];

[myDictionary setObject:valueArray forKey:keyArray];
NSLog(@"myDictionary: %@", myDictionary);

Example of [NSMutableDictionary dictionaryWithCapacity].
NSMutableDictionary *settings= [NSMutableDictionary dictionaryWithCapacity:10];
[settings setObject:@"2400" forKey:@"test"];

NSMutableDictionary dictionaryWithCapacity example.
self.venues = [NSArray arrayWithObjects:@"GSC One Utama",@"GSC Midvalley",@"TGV KLCC", @"TGV Sunway Pyramid", nil];

NSDictionary *venueDict = [NSDictionary dictionaryWithObject:self.venues forKey:@"venue:"];

NSMutableDictionary *mutableVenueDict = [NSMutableDictionary dictionaryWithCapacity:10];
[mutableVenueDict setObject:self.venues forKey:@"venue:"];

End of NSMutableDictionary dictionaryWithCapacity example article.