• 字典可变


    #import <Foundation/Foundation.h>
    
    int main(int argc, const char * argv[]) {
        @autoreleasepool {
    //可变字典创建
            NSMutableDictionary *mudic=[[NSMutableDictionary alloc]initWithObjectsAndKeys:@"jay",@"name",@"22",@"age",@"F",@"gender", nil];
        //插入
            NSDictionary *dic=[NSDictionary dictionaryWithObject:@"166" forKey:@"height"];
            [mudic addEntriesFromDictionary:dic];
            [mudic setValue:@"66" forKey:@"weight"];
            NSLog(@"%@",mudic);
        //创建新的字典,向空字典赋值
            NSMutableDictionary *newdic=[NSMutableDictionary dictionary];
            [newdic setDictionary:mudic];
            NSMutableDictionary *newdic1=[NSMutableDictionary dictionary];
            [newdic1 setDictionary:mudic];
            NSLog(@"%@",newdic);
        //将字典中对应key值删除(1)
            [newdic removeObjectForKey:@"weight"];
            NSLog(@"%@",newdic);
        //(2)利用数组
            NSArray *keys=[NSArray arrayWithObjects:@"height",@"gender", nil];
            [newdic removeObjectsForKeys:keys];
            NSLog(@"%@",newdic);
        //全部删除
            [newdic removeAllObjects];
            NSLog(@"%@",newdic);
    //遍历
    //先找到所有key值,计算key的个数,用以循环遍历条件;通过key的数组找到对应key的值
            NSArray *keyss=[newdic1 allKeys];
            //NSInteger count=[keyss count];
            for (int i=0; i<keyss.count; i++) {
                id key=[keyss objectAtIndex:i];//对应位置
                NSLog(@"%@",[newdic1 objectForKey:key]);
            }
    //快速枚举
            for (id key in newdic1) {
                id obj=[newdic1 objectForKey:key];
                NSLog(@"%@",obj);
            }
    //枚举对象 通过枚举对象进行枚举遍历
    //1.将字典里的key转成枚举对象,用于遍历(1)
            NSEnumerator *keyenum=[newdic keyEnumerator];
            id key;
            while (key=[keyenum nextObject]) {//从key中取值
                id obj=[newdic objectForKey:key];
                NSLog(@"%@",obj);
    
            }
        //(2)
            NSEnumerator *keyenum1=[newdic keyEnumerator];
            id key1=[keyenum1 nextObject];
            while (key1) {//从key中取值
                id obj1=[newdic objectForKey:key];
                NSLog(@"%@",obj1);
                key1=[keyenum1 nextObject];
            }
            
            
        }
        return 0;
    }
  • 相关阅读:
    IE6-9中tbody的innerHTML不能赋值bug
    matchesSelector及低版本IE中对该方法的实现
    JavaScript日期组件的实现
    IE6/7/8中parseInt第一个参数为非法八进制字符串且第二个参数不传时返回值为0
    子程序设计原则
    仅IE6中链接A的href为javascript协议时不能在当前页面跳转
    JavaScript获取图片的原始尺寸
    JavaScript判断图片是否加载完成的三种方式
    Mac OS X 快捷键
    IE6-8中Date不支持toISOString方法
  • 原文地址:https://www.cnblogs.com/haitong-0311/p/5120856.html
Copyright © 2020-2023  润新知