Objective-C( Foundation架構 一 NSDictionary)

來源:互聯網
上載者:User

標籤:

NSDictionary

不可變的字典

建立字典的方法

        // 建立字典的方式        NSDictionary *dy = [NSDictionary dictionaryWithObject:@"a"forKey:@"b"];        NSDictionary *dy1 = [NSDictionary dictionaryWithObjectsAndKeys:@"a",@"b",@"c",@"d" ,nil];                // 快速建立字典        // 重複key值,重複的不能儲存到字典中        NSDictionary *dy2 = @{@"we":@"haha", @"we":@"ha"};                NSLog(@"%@",dy);        NSLog(@"%@",dy1);        NSLog(@"%@",dy2);

 擷取字典的長度

        // 擷取字典長度        NSDictionary *dy2 = @{@"we":@"haha", @"w":@"ha"};        NSLog(@"%lu",dy2.count); // 輸出2           // 如果key值重複,長度不計算在內        NSDictionary *dy2 = @{@"we":@"haha", @"we":@"ha"};        NSLog(@"%lu",dy2.count); //輸出1 

字典的遍曆

        NSDictionary *dy1 = [NSDictionary dictionaryWithObjectsAndKeys:@"a",@"b",@"c",@"d" ,nil];                // 快速建立字典        // 重複key值,重複的不能儲存到字典中        NSDictionary *dy2 = @{@"we":@"haha", @"w":@"ha"};        NSLog(@"%lu",dy2.count);                // 字典的遍曆        for (NSString *key in dy2) {            NSLog(@"key = %@, value = %@", key, [dy2 objectForKey:key]);        }        // 枚舉類型遍曆        [dy1 enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {            NSLog(@"key = %@, value = %@", key, obj);        }];

把字典存到檔案中

        // 把字典儲存到檔案中        NSDictionary *dy2 = @{@"we":@"haha", @"w":@"ha"};        BOOL isWrite = [dy2 writeToFile:@"/Users/cloudwalk/Desktop/test.plist" atomically:YES];        if (isWrite) {            NSLog(@"寫入成功");        }

從檔案中讀取字典

        NSDictionary *readDy2 = [NSDictionary dictionaryWithContentsOfFile:@"/Users/cloudwalk/Desktop/test.plist"];        NSLog(@"%@",readDy2);        

通過把數組添加到字典中,構建成數組字典

        NSArray *sdArr = [NSArray arrayWithObjects:@"zaozhuang",@"jinan",nil];        NSArray *jxArr = [NSArray arrayWithObjects:@"jiujiang",@"nanchang",nil];        NSDictionary *citys = [NSDictionary dictionaryWithObjectsAndKeys:sdArr,@"sd",jxArr,@"jx", nil];        NSLog(@"citys = %@",citys);

 

        NSDictionary *citys = [NSDictionary dictionaryWithObjectsAndKeys:sdArr,@"sd",jxArr,@"jx", nil];        NSLog(@"citys = %@",citys);        // 把citys存到檔案中        [citys writeToFile:@"/Users/cloudwalk/Desktop/test1.plist" atomically:YES];

 

Objective-C( Foundation架構 一 NSDictionary)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.