zoukankan      html  css  js  c++  java
  • OC之【NSDictionary详解】

    main.m文件

    #import <Foundation/Foundation.h>

    #import "Student.h"

    #pragma mark 字典的初始化

    void dictCreate() {

        // NSDictionary是不可变的

        NSDictionary *dict = [NSDictionarydictionaryWithObject:@"v"forKey:@"k"];

        

        //最常用的初始化方式

        dict = [NSDictionarydictionaryWithObjectsAndKeys:

               @"v1", @"k1",

               @"v2", @"k2",

               @"v3", @"k3",nil];

        

       NSArray *objects = [NSArrayarrayWithObjects:@"v1",@"v2", @"v3",nil];

       NSArray *keys = [NSArrayarrayWithObjects:@"k1",@"k2", @"k3",nil];

        dict = [NSDictionarydictionaryWithObjects:objects forKeys:keys];

       NSLog(@"%@", dict);

    }

    #pragma mark 字典的基本用法

    void dictUse() {

        NSDictionary *dict = [NSDictionarydictionaryWithObjectsAndKeys:

               @"v1", @"k1",

               @"v2", @"k2",

               @"v3", @"k3",nil];

        

        // count是计算有多少个键值对(key-value)

       NSLog(@"count=%zi", dict.count);

        

        //由于NSDictionary是不可变的,所以只能取值,而不能修改值

       id obj = [dict objectForKey:@"k2"];

       NSLog(@"obj=%@", obj);

        

        //将字典写入文件中

        NSString *path =@"/Users/apple/Desktop/dictionary.xml";

        [dict writeToFile:pathatomically:YES];

        

        //从文件中读取内容

        dict = [NSDictionarydictionaryWithContentsOfFile:path];

       NSLog(@"dict=%@", dict);

    }

    #pragma mark 字典的用法

    void dictUse2() {

        NSDictionary *dict = [NSDictionarydictionaryWithObjectsAndKeys:

                             @"v1", @"k1",

                             @"v2", @"k2",

                             @"v3", @"k3",nil];

        // 返回所有的key

       NSArray *keys = [dict allKeys];

        //NSLog(@"keys=%@", keys);

        

       NSArray *objects = [dict allValues];

        //NSLog(@"objects=%@", objects);

        

        // 根据多个key取出对应的多个value

        // 当key找不到对应的value时,用marker参数值代替

        objects = [dict objectsForKeys:[NSArrayarrayWithObjects:@"k1",@"k2", @"k4", nil]notFoundMarker:@"not-found"];

       NSLog(@"objects=%@", objects);

    }

    #pragma mark 遍历字典

    void dictFor() {

        NSDictionary *dict = [NSDictionarydictionaryWithObjectsAndKeys:

                             @"v1", @"k1",

                             @"v2", @"k2",

                             @"v3", @"k3",nil];

        // 遍历字典的所有key

       for (id keyin dict) {

           id value = [dict objectForKey:key];

           NSLog(@"%@=%@", key, value);

        }

    }

    #pragma mark 遍历字典2

    void dictFor2() {

        NSDictionary *dict = [NSDictionarydictionaryWithObjectsAndKeys:

                             @"v1", @"k1",

                             @"v2", @"k2",

                             @"v3", @"k3",nil];

        // key迭代器

       NSEnumerator *enumer = [dict keyEnumerator];

       id key = nil;

       while ( key = [enumer nextObject]) {

           id value = [dict objectForKey:key];

           NSLog(@"%@=%@", key, value);

        }

        

        // 对象迭代器

        // [dict objectEnumerator];

    }

    #pragma mark 遍历字典3

    void dictFor3() {

        NSDictionary *dict = [NSDictionarydictionaryWithObjectsAndKeys:

                             @"v1", @"k1",

                             @"v2", @"k2",

                             @"v3", @"k3",nil];

        [dict enumerateKeysAndObjectsUsingBlock:

         ^(id key,id obj, BOOL *stop) {

           NSLog(@"%@=%@", key, obj);

        }];

    }

    #pragma mark 

    void dictMemory() {

       Student *stu1 = [StudentstudentWithName:@"stu1"];

       Student *stu2 = [StudentstudentWithName:@"stu2"];

       Student *stu3 = [StudentstudentWithName:@"stu3"];

        

        //一个对象称为字典的key或者value时,会做一次retain操作,也就是计数器会+1

        NSDictionary *dict = [NSDictionarydictionaryWithObjectsAndKeys:

                              stu1,@"k1",

                              stu2,@"k2",

                              stu3,@"k3", nil];

        

        //当字典被销毁时,里面的所有key和value都会做一次release操作,也就是计数器会-1

    }

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

    {

        @autoreleasepool {

           dictMemory();

        }

       return 0;

    }

    Student.h文件

    #import <Foundation/Foundation.h>

    @interface Student :NSObject

    @property (nonatomic,retain) NSString *name;

    + (id)studentWithName:(NSString *)name;

    @end

    Student.m文件

    #import "Student.h"

    @implementation Student

    + (id)studentWithName:(NSString *)name {

       Student *stu = [[Studentalloc] init];

        stu.name = name;

       return [stu autorelease];

    }

    - (void)dealloc {

       NSLog(@"%@被销毁了", _name);

        // 释放name

        [_namerelease];

        [superdealloc];

    }

    @end

    On the road。。。
  • 相关阅读:
    洛谷[ZJOI2008]骑士(基环树二次DP法+树形DP)
    洛谷P5022 旅行(基环树+断环法)
    AtCoder Beginner Contest 174 ——D.Alter Altar(思维)
    洛谷P1972 [SDOI2009]HH的项链(离线+树状数组)
    CF1365D Solve The Maze (BFS)
    codeforces1426——F. Number of Subsequences(DP)
    codeforces1324——E.Sleeping Schedule(DP+初始化)
    codeforces319——B. Psychos in a Line(思维+单调栈)
    codeforces292——D. Connected Components(并查集+前缀和优化)
    codeforces1013——D. Chemical table(思维+转化+并查集)
  • 原文地址:https://www.cnblogs.com/ianhao/p/4426122.html
Copyright © 2011-2022 走看看