OC语言day08-08不可变字典NSDictionary

pragma mark 不可变字典NSDictionary

pragma mark 概念

/**
 NSDictionary
 1.创建
字典_单个键值对 (dictionaryWithObject:forKey)
字典_多个个键值对 (dictionaryWithObjects:forKeys)

 2.取值
 字典_单个值 获取  (objectForKey)  字典_单个值 快速获取 dict[@"value"];
 字典_多个键值对 @{@"v1":@"k1",@"v2",@"k2"}
 
 3. 获取字典的键值对 个数
 (dict.count)
 
 4.遍历字典
 4.1for
 
 for (int i = 0; i

pragma mark 代码

#import 
#pragma mark 类

#pragma mark main函数
int main(int argc, const char * argv[])
{
 
#pragma mark 不可变字典NSDictionary
    
#warning 1.如何创建 (dictionaryWithObject:forKey)
    // obj 值 \
        key 键
    NSDictionary *dict = [NSDictionary dictionaryWithObject:@"lyh" forKey:@"name"];
#warning 1.1.取值 (objectForKey)
    NSString *name = [dict objectForKey:@"name"];
    NSLog(@"%@",name);
    
#warning 2. 创建多个键值对
    // 注意: key 和 value 是一一对应的
    NSDictionary *dict1 = [NSDictionary dictionaryWithObjects:@[@"lyh",@"24",@"172"] forKeys:@[@"name",@"age",@"height"]];
    NSLog(@"%@ %@ %@",[dict1 objectForKey:@"name"],[dict1 objectForKey:@"age"],[dict1 objectForKey:@"height"]);

#warning 3.简单创建 字典 @{键:值}
//    NSDictionary *dict2 = @{key:value};
    NSDictionary *dict2 = @{@"name":@"lyh"};
    NSLog(@"%@",[dict2 objectForKey:@"name"]);
    NSLog(@"%@",dict2[@"name"]);
    
 
#warning 4.创建多个 键值对 字典
    NSDictionary *dict3 = @{@"name":@"lyh",@"age":@"25",@"height":@"170"};
    NSLog(@"%@ %@ %@",dict3[@"name"],dict3[@"age"],dict3[@"height"]);
    
    
#pragma mark 5.字典的遍历
#warning 5.1 获取键值对
    dict = @{@"name":@"lys",@"age":@"28",@"height":@"171"};
    // 如何获取字典中 key 和 value的个数, 在字典中 key称之为 键, value称之为 值
    NSLog(@"count = %lu",dict.count);
 
    
#warning 5.2 字典遍历
    // for
#warning 5.2.1 字典遍历 (for)
    for (int i = 0; i

你可能感兴趣的:(OC语言day08-08不可变字典NSDictionary)