NSMutableDictionary setObject forKeyedSubscript
- (void)setObject:(id)object forKeyedSubscript:(id < NSCopying >)aKey
Parameters of [NSMutableDictionary setObject forKeyedSubscript]
object
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.
Discussion of [NSMutableDictionary setObject forKeyedSubscript]
This method is identical to setObject:forKey:.
NSMutableDictionary setObject forKeyedSubscript example.
NSMutableDictionary* dict = [[NSMutableDictionary alloc] initWithDictionary:delegate.sharedData.dictFaves];
[dict setObject:@"test" forKeyedSubscript:@"4"];
delegate.sharedData.dictFaves = dict;
[dict release];
[dict setObject:@"test" forKeyedSubscript:@"4"];
delegate.sharedData.dictFaves = dict;
[dict release];
Example of [NSMutableDictionary setObject forKeyedSubscript].
- (BOOL)addStudent:(Student *)newStudent
{
NSLog(@"adding new student");
if (studentStore == nil) {
studentStore = [[NSMutableDictionary alloc] init];
}
[studentStore setObject:newStudent forKeyedSubscript:newStudent.adminNo];
return YES;
}
{
NSLog(@"adding new student");
if (studentStore == nil) {
studentStore = [[NSMutableDictionary alloc] init];
}
[studentStore setObject:newStudent forKeyedSubscript:newStudent.adminNo];
return YES;
}
NSMutableDictionary setObject forKeyedSubscript 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 forKeyedSubscript:myWord];
}
else
{
[myDictionary setObject:[NSNumber numberWithInt:1] forKeyedSubscript:myWord]
}
NSMutableDictionary *myDictionary = [[NSMutableDictionary alloc] init];
NSNumber *value = [myDictionary objectForKey:myWord];
if (value)
{
NSNumber *nextValue = [NSNumber numberWithInt:[value intValue] + 1];
[myDictionary setObject:nextValue forKeyedSubscript:myWord];
}
else
{
[myDictionary setObject:[NSNumber numberWithInt:1] forKeyedSubscript:myWord]
}