• 可变字典


    //

    //  main.m

    //  可变字典

    //

    //  Created by 博博 on 16/1/8.

    //  Copyright (c) 2016年 com.bb. All rights reserved.

    //

    #define NSLog(FORMAT, ...) printf("%s ", [[NSString stringWithFormat:FORMAT, ##__VA_ARGS__] UTF8String])

    #import <Foundation/Foundation.h>

    int main(int argc, const char * argv[]) {

        @autoreleasepool {

            

            

            

            

            NSMutableDictionary *mudic=[[NSMutableDictionary alloc]initWithObjectsAndKeys:@"jay",@"name",@"22",@"age",@"f",@"gender", nil];

            //NSLog(@"%@",mudic);

            NSDictionary *dic=[NSDictionary dictionaryWithObject:@"166" forKey:@"height"];//法1

            [mudic addEntriesFromDictionary:dic];                                         //法1

            [mudic setValue:@"66" forKey:@"weight"];                                      //法2

            //NSLog(@"%@",mudic);

            

            NSMutableDictionary *nulldic=[NSMutableDictionary dictionary];

            //将字典nulldic设置与字典mudic对象相同

            [nulldic setDictionary:mudic];

            //NSLog(@"%@",nulldic);

            //将字典中对应key的值删除

            [nulldic removeObjectForKey:@"weight"];

            //NSLog(@"%@",nulldic);

            NSArray *keys=[NSArray arrayWithObjects:@"heighr",@"gender", nil];

            [nulldic removeObjectsForKeys:keys];

            //NSLog(@"%@",nulldic);

            [nulldic removeAllObjects];

            //NSLog(@"%@",nulldic);

            //遍历

            [nulldic setDictionary:mudic];

            //1.先找到所有key;2.计算key的个数,用于循环遍历条件;3通过key 的数组找到对应key值

            NSArray *keyss=[nulldic allKeys];

            NSInteger count=[nulldic count];

            for(int i=0;i<nulldic.count;i++)

            {

                id key=[keyss objectAtIndex:i];

                //NSLog(@"%@",[nulldic objectForKey:key]);

            }

            //2.快速枚举

            for (id key in nulldic)

            {

                id obj=[nulldic objectForKey:key];

                NSLog(@"%@",obj);

            }

            //3.枚举对象(c语言中的枚举enum) 通过枚举对象进行枚举

            //将字典里的key转成枚举对象,用于遍历/枚举

            NSLog(@"/////////");

            NSEnumerator *keyenum=[nulldic keyEnumerator];

            id key=[keyenum nextObject];

            while (key=[keyenum nextObject]) {

                

                id obj=[nulldic objectForKey:key];

                NSLog(@"%@",obj);

                key=[keyenum nextObject];

                

            }

        }

        return 0;

    }

  • 相关阅读:
    CSS-常用hack
    CSS触发haslayout的方法
    CSS最大最小宽高兼容
    CSS-文字超出自动显示省略号
    [LeetCode][JavaScript]Number of Islands
    [LeetCode][JavaScript]Search a 2D Matrix II
    [LeetCode][JavaScript]Search a 2D Matrix
    [LeetCode][JavaScript]Candy
    [LeetCode][JavaScript]Wildcard Matching
    [LeetCode][JavaScript]Sliding Window Maximum
  • 原文地址:https://www.cnblogs.com/bobohahaha/p/5120934.html
Copyright © 2020-2023  润新知