12-11 字典(NSDictionary和NSMutableDictionary

#import 
//  字典:NSDictionary和NSMutableDictionary
int main(int argc, const char * argv[]) {
    @autoreleasepool {
        //  只读字典
        NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:@"第一个元素的Value", @"value1",@"第二个元素的Value", @"value2", @"第三个元素的value", @"value3",nil];
        //遍历字符串
        for(NSString *key in dictionary)
        {
            NSLog(@"%@:%@", key, [dictionary objectForKey:key]);
             NSLog(@"%@:%@", key, dictionary[key]);
        }
        
        //  可读写的字典
        NSMutableDictionary *mutableDictionary = [NSMutableDictionary dictionary];
        mutableDictionary[@"key1"] = @"first value";
        mutableDictionary[@"key2"] = @"second value";
        mutableDictionary[@"key3"] = @"third value";
        
        NSLog(@"key2 = %@", mutableDictionary[@"key2"]);

        [mutableDictionary removeObjectForKey:@"key3"];
        for(NSString *key in mutableDictionary)
        {
            NSLog(@"%@:%@", key, [mutableDictionary objectForKey:key]);//打印的时候与实例化的顺序无关
            
        }
    }
    return 0;
}
12-11 字典(NSDictionary和NSMutableDictionary_第1张图片
12-11 字典(NSDictionary和NSMutableDictionary.png

你可能感兴趣的:(12-11 字典(NSDictionary和NSMutableDictionary)