Objective-C字典(17-08-01)

//
//  main.m
//  OC05_字典
//
//  Created by lanou3g on 17/8/1.
//  Copyright © 2017年 lanou3g. All rights reserved.
//

#import 

int main(int argc, const char * argv[]) {
    
    NSDictionary *dic = @{@"name":@"lee",@"age":@"20"};
    //查看字典中键值对个数
    NSLog(@"%lu",dic.count);
    //字典的一种遍历方式
    NSArray *keyArray = dic.allKeys;
    for (int i = 0; i < keyArray.count; i++) {
        NSString *key = keyArray[i];
        NSString *value = [dic objectForKey:key];
        NSLog(@"key = %@,value = %@",key,value);
    }
    //可变字典
    //创建一个空字典
    NSMutableDictionary *mutableDic = [NSMutableDictionary dictionary];
    //创建有键值对的字典
    NSMutableDictionary *mutableDic1 = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"ning",@"name",@"24",@"age", nil];
    NSMutableDictionary *mutableDic2 = [@{@"name":@"包包",@"age":@"24"} mutableCopy];
    [mutableDic setObject:@"ying" forKey:@"name"];
    [mutableDic removeObjectForKey:@"name"];
    NSLog(@"%@",mutableDic);
    //清空字典
    [mutableDic1 removeAllObjects];
    NSLog(@"%@",mutableDic1);
    
    return 0;
}

你可能感兴趣的:(Objective-C字典(17-08-01))