• 【Foundation Frame】NSDictionary/NSMutableDictionary


    NSDictionary 用于保存具有映射关系的数据,NSDictionary 包含两组数据,一组用来保存key值,一组用来保存valure值。
    一个Key可以对应多个value,反过来则不可以。
    //
    //  main.m
    //  NSDictionary
    //
    //  Created by mac on 14-12-4.
    //  Copyright (c) 2014 mac. All rights reserved.
    //

    #import<Foundation/Foundation.h>


    void staticCreatDictionary()
    {
       
    NSDictionary * dict = [NSDictionarydictionary];
        dict = [
    NSDictionarydictionaryWithObject:@"dictonary0"forKey:@"key1"];
        dict = [
    NSDictionarydictionaryWithObjectsAndKeys:@"Dictionary1",@"key2",@"Dictionary2",@"key3",@"Dictionary3",@"key4",nil];
       
       
    NSDictionary * dict1 = [NSDictionarydictionaryWithDictionary:dict];
       
       
    NSLog(@"DICTIONARY:%@",dict1);
       
       
    NSArray * keyArray = [NSArrayarrayWithObjects:@"k1",@"k2",@"k3",@"k4",@"k5",nil];
       
    NSArray * valureArray = [NSArrayarrayWithObjects:@"Hello1",@"Hello2",@"Hello3",@"Hello4",@"Hello5",nil];
       
    //通过数组创建
        dict = [
    NSDictionarydictionaryWithObjects:valureArrayforKeys:keyArray];
       
    NSLog(@"%@",dict);
       
       
    //通过文件创建
       
    NSDictionary * dict3 ;
        dict3 = [
    NSDictionarydictionaryWithContentsOfFile:@"/Users/mac/desktop/a.txt"];
       
    NSLog(@"Ditc3:%@",dict3);
       
    }

    void others()
    {  
    NSDictionary * dict ;
        dict = [
    NSDictionarydictionaryWithObjectsAndKeys:@"Dictionary1",@"key2",@"Dictionary2",@"key3",@"Dictionary3",@"key4",nil];
       
    //计数字典大小
       
    NSUInteger count = [dictcount];
       
    NSLog(@"%lu",count);
       
       
    //判断是否两个字典是否相同
       
    NSDictionary * dict1;
        dict1 = [
    NSDictionarydictionaryWithObject:@"dictonary0"forKey:@"key1"];
       
    NSLog(@"%i",[dictisEqualToDictionary:dict1]);
       
        [dict
    writeToFile:@"/Users/mac/desktop/a.txt"atomically:YES];
       
    }

    void keyAndValure()
    {
       
    NSDictionary * dict3 ;
        dict3 = [
    NSDictionarydictionaryWithContentsOfFile:@"/Users/mac/desktop/a.txt"];
       
    //返回所有key
       
    NSArray * keysAtrray = [dict3allKeys];
       
    NSLog(@"%@",keysAtrray);
       
    //返回一个对象对应的所有key
       
    NSArray * keyArray = [dict3allKeysForObject:@"Dictionary1"];
       
    NSLog(@"%@",keyArray);
       
       
    //返回一个key对应的valure
       
    id obj = [dict3objectForKey:@"key2"];
       
    NSLog(@"%@",obj);
       
    //返回一组key对应的valure
       
    NSArray * valureArray = [dict3objectsForKeys:keysAtrraynotFoundMarker:@"NoFound"];
       
    NSLog(@"%@",valureArray);
       
    }

    void  ergodic()
    {
       
    NSDictionary * dict3 ;
        dict3 = [
    NSDictionarydictionaryWithContentsOfFile:@"/Users/mac/desktop/a.txt"];
       
    //快速遍历
       
    for (id key in dict3) {
           
    NSLog(@"%@",[dict3objectForKey:key]);
        }
       
    //迭代器方法遍历
       
    NSEnumerator * keyEnum = [dict3keyEnumerator];
       
    id obj;
       
    while (obj = [keyEnumnextObject]) {
           
    NSLog(@"%@",obj);
        }
       
    NSEnumerator * valureEnum = [dict3objectEnumerator];
       
    id obj1;
       
    while (obj1 = [valureEnumnextObject]) {
           
    NSLog(@"%@",obj1);
        }
       
       
    //block方法遍历
        [dict3
    enumerateKeysAndObjectsUsingBlock:^(id key, id obj,BOOL *stop) {
           
    NSLog(@"%@  %@",key,obj);
           
    NSString * keyJuge = key ;
           
    if ([keyJugecompare:@"key3"]==0) {
                *stop =
    YES ;
            }
        }];
       
    }


    void mutableDictionary()
    {
       
    //动态创建
       
    NSMutableDictionary * dynamicDict;
       
    NSArray * valueArray = [NSArrayarrayWithObjects:@"Hello1",@"Hello2",@"Hello3",@"Hello4",nil];
       
    NSArray * keyArray = [NSArrayarrayWithObjects:@"1",@"2",@"3",@"4",nil];
        dynamicDict = [
    NSMutableDictionarydictionaryWithObjects:valueArrayforKeys:keyArray];
       
    NSLog(@"%@",dynamicDict);
       
       
    //替换元素元素
        [dynamicDict
    setObject:@"What"forKey:@"1"];
       
    //删除元素
        [dynamicDict
    removeObjectForKey:@"1"];
        NSLog(@"%@",dynamicDict);
    }

    int main(int argc, constchar * argv[]) {
       
    @autoreleasepool {
           
    // insert code here...
           
    NSLog(@"Hello, World!");
         
    //  staticCreatDictionary();
         
    //  others();
         
    //   keyAndValure();
          
    // ergodic();
           
    mutableDictionary();
        }
       
    return0;
    }
  • 相关阅读:
    模块
    关于使用 jBox 对话框的提交问题
    Julia is a high-level, high-performance dynamic programming language for technical computing, with syntax that is familiar to users of other technical
    Julia语言:让高性能科学计算人人可用
    [ASP.NET Web API]如何Host定义在独立程序集中的Controller
    ASHX呼叫ASPX.cs的方法
    robotlegs2.0框架实例源码带注释
    指针指向一块内存,它的内容是所指内存的地址;而引用则是某块内存的别名(转)
    老板该如何正确的犒赏程序员(转)
    Jass 技能模型定义(转)
  • 原文地址:https://www.cnblogs.com/shujucn/p/7481459.html
Copyright © 2020-2023  润新知