美文网首页
Objective-C字典(17-08-01)

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

作者: Hilarylii | 来源:发表于2017-08-01 17:07 被阅读0次
    //
    //  main.m
    //  OC05_字典
    //
    //  Created by lanou3g on 17/8/1.
    //  Copyright © 2017年 lanou3g. All rights reserved.
    //
    
    #import <Foundation/Foundation.h>
    
    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)

          本文链接:https://www.haomeiwen.com/subject/zwajlxtx.html