首页 > 白话Objective-C > 白话Objective-C之NSMutableDictionary

白话Objective-C之NSMutableDictionary

NSDictionary是不可变的,一旦创建,不可以再修改。NSMutableDictionary是可变的,创建完还可以再修改。

NSMutableDictionary一些常用用法的示例:

//创建MutableDictionary
NSMutableDictionary *mutableDictionary=[NSMutableDictionary dictionaryWithObjectsAndKeys:@"Object1",@"key1",@"Object2",@"key2",nil];
//添加对象
[mutableDictionary setObject:@"Object3" forKey:@"key3"];
//修改对象也是用setObject
NSString *string = @"Object 1 has been changed";
[mutableDictionary setObject:string forKey:@"key1"];

//删除对象
[mutableDictionary removeObjectForKey:@"key1"];
//删除多个对象
NSArray *keyArray =[NSArray arrayWithObjects:@"key2",@"key3", nil];
[mutableDictionary removeObjectsForKeys:keyArray];
//删除所有对象
[mutableDictionary removeAllObjects];

危险的dictionaryWithObjectsAndKeys

/*危险的dictionaryWithObjectsAndKeys
 我们知道dictionary是以nil为结束,当object1为nil的时候,dictionary已经结束,此时dictionary会忽略掉后面的key value
 在取我们认为肯定不为nil的key2时,object2也为nil,
 有可能引发各种意外。*/
NSString *object1 = nil;
NSString *object2 = @"object2";
NSMutableDictionary *dangerousDictionary=[NSMutableDictionary dictionaryWithObjectsAndKeys:object1,@"key1",object2,@"key2",nil];
//解决方法一:object有可能为nil的时候,采用setObject:forKey:
NSString *object1 = nil;
NSString *object2 = @"object2";
NSMutableDictionary *dangerousDictionary = [[NSMutableDictionary alloc]init];
if (object1) {
    [dangerousDictionary setObject:object1 forKey:@"key1"];
}
if (object2) {
    [dangerousDictionary setObject:object2 forKey:@"key2"];
}
[dangerousDictionary setObject:@"object3" forKey:@"key3"];
//此时[dangerousDictionary objectForKey:@"key1"]的值为nil, 打印出(null)
NSString *objectOfKey1  = [dangerousDictionary objectForKey:@"key1"];
NSLog(@"object of key1 = %@",objectOfKey1);
//解决方法二:采用setValue:forKey:
/*
setValue:forKey:与setObject:forKey:不完全等同,最大的区别有两点:
1. setValue:forKey:只接受NSString*类型的key
2. setValue:forKey:当value为nil时,将调用removeObjectForKey:
*/
NSString *object1 = nil;
NSString *object2 = @"object2";
NSMutableDictionary *dangerousDictionary = [[NSMutableDictionary alloc]init];
[dangerousDictionary setValue: object1 forKey:@"key1"];
[dangerousDictionary setValue:object2 forKey:@"key2"];
[dangerousDictionary setValue:@"object2" forKey:@"key3"];
//此时[dangerousDictionary objectForKey:@"key1"]的值为nil, 打印出(null)
NSString *objectOfKey1  = [dangerousDictionary objectForKey:@"key1"];
NSLog(@"object of key1 = %@",objectOfKey1);
//方法三,若值为nil,还想要保存进dictionary,可以使用[NSNull null]
NSString *object1 = nil;
NSString *object2 = @"object2";
NSMutableDictionary *dangerousDictionary = [[NSMutableDictionary alloc]init];
if (object1) {
    [dangerousDictionary setObject:object1 forKey:@"key1"];
} else {
    [dangerousDictionary setObject:[NSNull null] forKey:@"key1"];
}
if (object2) {
    [dangerousDictionary setObject:object2 forKey:@"key2"];
} else {
    [dangerousDictionary setObject:[NSNull null] forKey:@"key2"];
}
[dangerousDictionary setObject:@"object3" forKey:@"key3"];
//此时[dangerousDictionary objectForKey:@"key1"]的值为一个对象[NSNull null],打印出<null>
NSString *objectOfKey1  = [dangerousDictionary objectForKey:@"key1"];
NSLog(@"object of key1 = %@",objectOfKey1);
//综合方法一,方法二,方法三,若想判断objectOfKey1是否非nil跟非[NSNull null],需要使用下面这样的判断.
if (objectOfKey1 != nil && (id)objectOfKey1 != [NSNull null]) {
    NSLog(@"objectOfkey1 is not nill and not equal to object [NSNull null]");
} else {
    if (objectOfKey1 == nil) {
        NSLog(@"objectOfKey1 is nil");
    } else {
        NSLog(@"objectOfKey1 is euqal to object [NSNull null]");
    }
}

(转载本站文章请注明出处 www.helloitworks.com ,请勿用于任何商业用途)

  1. 本文目前尚无任何评论.
  1. 本文目前尚无任何 trackbacks 和 pingbacks.