NSDictionary 常用方法学习

来源:互联网 发布:电脑网络查询 编辑:程序博客网 时间:2024/06/06 20:07
////  NSDictionaryStu.m//  OC基础学习////  Created by 麦子 on 15/4/13.//  Copyright (c) 2015年 麦子. All rights reserved.//#import "NSDictionaryStu.h"@implementation NSDictionaryStu-(void) dictionaryStu{    NSLog(@"NSDictionary 学习");        /**           这个类就相当于java中的map类, 键值对的形式存在, key 和  value,  字典对象中的键和值不可以为空(nil), 如果需要的话需要使用NSNull,这种包装类。            它也有可变字典类,子类。  NSMutableDictionary,   数组用nil来进行结束     */            /*************** 初始化**************/        NSDictionary  *dictionary = [NSDictionary dictionaryWithObject:@"value-1" forKey:@"key-1"]; // 单个创建    NSLog(@"dictionary-1----%@",dictionary);            NSDictionary *dictionaryArray = [NSDictionary dictionaryWithObjectsAndKeys:@"val-1",@"key-1",@"val-2",@"key-2", nil];//创建多个    NSLog(@"dictionary-2-----%@",dictionaryArray);        NSDictionary *dictionaryCopy = [NSDictionary dictionaryWithDictionary:dictionaryArray];// copy 原来的数组    NSLog(@"dictionary-3----%@",dictionaryCopy);                // 获取字典数量    int number = [dictionaryArray count];    NSLog(@"---%d",number);        // 通过key获取val    NSString  *val = [dictionaryArray objectForKey:@"key-1"];    NSLog(@"val---%@",val);            // 获取所有的key和val    NSArray  *arrayKeys = [dictionaryArray allKeys];    NSArray  *arrayVals = [dictionaryArray allValues];    NSLog(@"key---%@----val----%@",arrayKeys,arrayVals);                /************** NSMutableDictionary 可变字典 *******/    NSMutableDictionary *mutableDictionary = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"obj-1",@"key-1",@"obj-2",@"key-2",@"obj-3",@"key-3",@"obj-4",@"key-4", nil];    NSDictionary *addDictionary = [NSDictionary dictionaryWithObject:@"obj-3" forKey:@"key-3"];    [mutableDictionary addEntriesFromDictionary:addDictionary]; // 添加一个字典,到原来的字典中,这个方法是返回void    NSLog(@"----%@",mutableDictionary);        [mutableDictionary setValue:@"maizi-today" forKey:@"key-3"];//重新设置值     NSLog(@"----%@",mutableDictionary);        // 放入另一个字典中    NSMutableDictionary  *dic = [NSMutableDictionary dictionary ]; // 空字典    [dic setDictionary:mutableDictionary];    NSLog(@"--********%@",dic);             // 删除    [mutableDictionary removeObjectForKey:@"key-1"];    NSLog(@"---%@",mutableDictionary);        // 删除一组    NSArray *array = [NSArray arrayWithObjects:@"key-3",@"key-4", nil];    [mutableDictionary removeObjectsForKeys:array];    NSLog(@"---%@",mutableDictionary);        // 删除所有    [mutableDictionary removeAllObjects];    NSLog(@"---%@",mutableDictionary);                // 循环字典    NSLog(@"%@",dic);        for (int index = 0; index < [dic count]; index++) {                id key = [[dic allKeys] objectAtIndex:index];        id obj = [dic objectForKey:key];                NSLog(@"---key--%@---val----%@---",key,obj);            }            for (id key in dic) {                id obj = [dic objectForKey:key];                NSLog(@"----**%@****----%@",key,obj);            }                id key;    NSEnumerator *enmum = [dic keyEnumerator];    while (key = [enmum nextObject]) {                id object = [dic objectForKey:key];        NSLog(@"*******----%@",object);    };}@end
<span style="font-family: Arial, Helvetica, sans-serif;">运行结果如下:</span>
<span style="font-family: Arial, Helvetica, sans-serif;"></span><pre name="code" class="plain">2015-04-15 23:54:28.428 OC基础学习[1179:75792] NSDictionary 学习2015-04-15 23:54:28.429 OC基础学习[1179:75792] dictionary-1----{    "key-1" = "value-1";}2015-04-15 23:54:28.429 OC基础学习[1179:75792] dictionary-2-----{    "key-1" = "val-1";    "key-2" = "val-2";}2015-04-15 23:54:28.429 OC基础学习[1179:75792] dictionary-3----{    "key-1" = "val-1";    "key-2" = "val-2";}2015-04-15 23:54:28.429 OC基础学习[1179:75792] ---22015-04-15 23:54:28.429 OC基础学习[1179:75792] val---val-12015-04-15 23:54:28.430 OC基础学习[1179:75792] key---(    "key-1",    "key-2")----val----(    "val-1",    "val-2")2015-04-15 23:54:28.430 OC基础学习[1179:75792] ----{    "key-1" = "obj-1";    "key-2" = "obj-2";    "key-3" = "obj-3";    "key-4" = "obj-4";}2015-04-15 23:54:28.430 OC基础学习[1179:75792] ----{    "key-1" = "obj-1";    "key-2" = "obj-2";    "key-3" = "maizi-today";    "key-4" = "obj-4";}2015-04-15 23:54:28.430 OC基础学习[1179:75792] --********{    "key-1" = "obj-1";    "key-2" = "obj-2";    "key-3" = "maizi-today";    "key-4" = "obj-4";}2015-04-15 23:54:28.448 OC基础学习[1179:75792] ---{    "key-2" = "obj-2";    "key-3" = "maizi-today";    "key-4" = "obj-4";}2015-04-15 23:54:28.449 OC基础学习[1179:75792] ---{    "key-2" = "obj-2";}2015-04-15 23:54:28.449 OC基础学习[1179:75792] ---{}2015-04-15 23:54:28.449 OC基础学习[1179:75792] {    "key-1" = "obj-1";    "key-2" = "obj-2";    "key-3" = "maizi-today";    "key-4" = "obj-4";}2015-04-15 23:54:28.449 OC基础学习[1179:75792] ---key--key-2---val----obj-2---2015-04-15 23:54:28.450 OC基础学习[1179:75792] ---key--key-1---val----obj-1---2015-04-15 23:54:28.450 OC基础学习[1179:75792] ---key--key-4---val----obj-4---2015-04-15 23:54:28.450 OC基础学习[1179:75792] ---key--key-3---val----maizi-today---2015-04-15 23:54:28.450 OC基础学习[1179:75792] ----**key-2****----obj-22015-04-15 23:54:28.450 OC基础学习[1179:75792] ----**key-1****----obj-12015-04-15 23:54:28.451 OC基础学习[1179:75792] ----**key-4****----obj-42015-04-15 23:54:28.451 OC基础学习[1179:75792] ----**key-3****----maizi-today2015-04-15 23:54:28.470 OC基础学习[1179:75792] *******----obj-22015-04-15 23:54:28.470 OC基础学习[1179:75792] *******----obj-12015-04-15 23:54:28.470 OC基础学习[1179:75792] *******----obj-42015-04-15 23:54:28.470 OC基础学习[1179:75792] *******----maizi-todayProgram ended with exit code: 0


                                             
0 0
原创粉丝点击