ios objective-c之-9:字典,可变字典 (NSDictonary NSMutableDictoray)

一:字典的基础讲解

NSDictionary(字典),NSDictionary类似于 .net中的parameter,l类似于java中的map。

通过唯一的key找到对应的值,一个key只能对应一个只,而多个key可以对应同一个值。NSDictionary 在初始化之后,就不可以再进行修改。

使用类方法创建NSDictionary对象。

初始化一个NSDictionary对象。使用+ (id)dictionaryWithObject:(id)object forKey:(id)key;

NSDictionary* dic = [NSDictionary dictionaryWithObject:@"values1" forKey:@"key1"];
NSLog(@"%@",dic);
//结果

2014-08-26 19:13:29.274 Nsdictonary[288:707] {

key1 = values1;

}

初始化一个NSDictionary对象。使用+ (id)dictionaryWithObjectsAndKeys:(id)firstObject, ... NS_REQUIRES_NIL_TERMINATION;

NSDictionary* dic = [NSDictionary dictionaryWithObjectsAndKeys:
                             @"values1",@"key1"
                             @"values2",@"key2"
                             @"values3",@"key3" ,nil];
   NSLog(@"%@",dic);、
//结果

初始化一个NSDictionary对象。使用+ (id)dictionaryWithObjects:(NSArray *)objects forKeys:(NSArray *)keys;

NSArray* values = [NSArray arrayWithObjects:@"values1",@"values2",@"values3", nil];
        NSArray* keys = [NSArray arrayWithObjects:@"key1",@"key2",@"key3", nil];
        NSDictionary* dic = [NSDictionary dictionaryWithObjects:values forKeys:keys];
NSLog(@"%@",dic);

结果:

2014-08-26 19:30:34.286 Nsdictonary[345:707] {

key1 = values1;

key2 = values2;

key3 = values3;

}

使用实例方法创建NSDictionary

创建一个空的字典:

NSDictionary* dic = [[NSDictionary alloc]init];

NSLog(@"%@",dic);

通过两个数组创建字典对象。

 NSArray* values = [NSArray arrayWithObjects:@"values1",@"values2",@"values3", nil];
        NSArray* keys = [NSArray arrayWithObjects:@"key1",@"key2",@"key3", nil];
        NSDictionary* dic = [[NSDictionary alloc] initWithObjects:values forKeys:keys];

        NSLog(@"%@",dic);

通过一个字典来创建一个新的字典。

 NSArray* values = [NSArray arrayWithObjects:@"values1",@"values2",@"values3", nil];
        NSArray* keys = [NSArray arrayWithObjects:@"key1",@"key2",@"key3", nil];
        NSDictionary* dic2 = [NSDictionary dictionaryWithObjects:values forKeys:keys];

        NSDictionary* dic = [[NSDictionary alloc] initWithDictionary:dic2];

        NSLog(@"%@",dic);

计算一个字典中有多少个键值对:??????

        NSArray* values = [NSArray arrayWithObjects:@"values1",@"values2",@"values3", nil];
        NSArray* keys = [NSArray arrayWithObjects:@"key1",@"key2",@"key3", nil];
        NSDictionary* dic2 = [NSDictionary dictionaryWithObjects:values forKeys:keys];

        NSDictionary* dic = [[NSDictionary alloc] initWithDictionary:dic2];
        NSLog(@"count :%lu",[dic count]);
        NSLog(@"%@",dic1);

结果:

2014-08-26 19:44:54.809 Nsdictonary[439:707] count :3

2014-08-26 19:44:54.817 Nsdictonary[439:707] {

key1 = values1;

key2 = values2;

key3 = values3;

}

通过健来去对应的值:

 NSObject* obj = [dic objectForKey:@"key1"];
        NSArray* values = [NSArray arrayWithObjects:@"values1",@"values2",@"values3", nil];
        NSArray* keys = [NSArray arrayWithObjects:@"key1",@"key2",@"key3", nil];
        NSDictionary* dic2 = [NSDictionary dictionaryWithObjects:values forKeys:keys];

        NSDictionary* dic = [[NSDictionary alloc] initWithDictionary:dic2];
        NSObject* obj = [dic objectForKey:@"key1"];
        NSLog(@"key1 = %@",obj);
结果:
2013-08-26 19:47:24.175 Nsdictonary[453:707] key1 = values1
将字典写入文件中:

 [dic writeToFile:path atomically:YES];
        NSArray* values = [NSArray arrayWithObjects:@"values1",@"values2",@"values3", nil];
        NSArray* keys = [NSArray arrayWithObjects:@"key1",@"key2",@"key3", nil];
        NSDictionary* dic2 = [NSDictionary dictionaryWithObjects:values forKeys:keys];

        NSDictionary* dic = [[NSDictionary alloc] initWithDictionary:dic2];
        NSString* path =@"/Users/administrator/Desktop/test.xml";
        NSLog(@"dic:%@",dic);
        [dic writeToFile:path atomically:YES];
        NSDictionary* dicTest = [NSDictionary dictionaryWithContentsOfFile:path];
        NSLog(@"dicTest: %@",dicTest);

结果:

2014-08-26 19:55:31.276 Nsdictonary[500:707] dic:{

key1 = values1;

key2 = values2;

key3 = values3;

}

2014-08-26 19:55:31.294 Nsdictonary[500:707] dicTest: {

key1 = values1;

key2 = values2;

key3 = values3;

}

返回所有的keys:

  NSArray* retKeys = [dic allKeys];:
        NSArray* values = [NSArray arrayWithObjects:@"values1",@"values2",@"values3", nil];
        NSArray* keys = [NSArray arrayWithObjects:@"key1",@"key2",@"key3", nil];
        NSDictionary* dic2 = [NSDictionary dictionaryWithObjects:values forKeys:keys];

        NSDictionary* dic = [[NSDictionary alloc] initWithDictionary:dic2];
        NSArray* retKeys = [dic allKeys];
        NSLog(@"all keys :%@",retKeys);

结果:

2013-08-26 19:58:48.871 Nsdictonary[515:707] all keys :(

key1,

key3,

key2

)

返回所有的值:allvalues

        NSArray* values = [NSArray arrayWithObjects:@"values1",@"values2",@"values3", nil];
        NSArray* keys = [NSArray arrayWithObjects:@"key1",@"key2",@"key3", nil];
        NSDictionary* dic2 = [NSDictionary dictionaryWithObjects:values forKeys:keys];

        NSDictionary* dic = [[NSDictionary alloc] initWithDictionary:dic2];
        NSArray* retValues = [dic allValues];
        NSLog(@"all keys :%@",retValues);

结果:

2014-08-26 19:59:57.768 Nsdictonary[532:707] all keys :(

values1,

values3,

values2

)


NSMutableDictionary  创建插入删除

创建一个

        NSArray* values = [NSArray arrayWithObjects:@"values1",@"values2",@"values3", nil];
        NSArray* keys = [NSArray arrayWithObjects:@"key1",@"key2",@"key3", nil];
        NSMutableDictionary* dic2 = [NSMutableDictionary dictionaryWithObjects:values forKeys:keys];

        NSMutableDictionary* dic = [[NSMutableDictionary alloc] initWithDictionary:dic2];
        NSLog(@"dic : %@",dic);
结果:

2014-08-26 20:11:56.388 Nsdictonary[634:707] dic : {

key1 = values1;

key2 = values2;

key3 = values3;

}

插入一个新的健值对:

[dic setObject:@"values4" forKey:@"key4"];
        NSArray* values = [NSArray arrayWithObjects:@"values1",@"values2",@"values3", nil];
        NSArray* keys = [NSArray arrayWithObjects:@"key1",@"key2",@"key3", nil];
        NSMutableDictionary* dic2 = [NSMutableDictionary dictionaryWithObjects:values forKeys:keys];

        NSMutableDictionary* dic = [[NSMutableDictionary alloc] initWithDictionary:dic2];
        NSLog(@"dic : %@",dic);
        [dic setObject:@"values4" forKey:@"key4"];
        NSLog(@"dic : %@",dic);
结果:

2014-08-26 20:15:36.330 Nsdictonary[680:707] dic : {

key1 = values1;

key2 = values2;

key3 = values3;

}

2013-08-26 20:15:36.338 Nsdictonary[680:707] dic : {

key1 = values1;

key2 = values2;

key3 = values3;

key4 = values4;

}

移除一个健值对:

[dic removeObjectForKey:@"key1"];
        NSArray* values = [NSArray arrayWithObjects:@"values1",@"values2",@"values3", nil];
        NSArray* keys = [NSArray arrayWithObjects:@"key1",@"key2",@"key3", nil];
        NSMutableDictionary* dic2 = [NSMutableDictionary dictionaryWithObjects:values forKeys:keys];

        NSMutableDictionary* dic = [[NSMutableDictionary alloc] initWithDictionary:dic2];
        NSLog(@"dic : %@",dic);
        [dic removeObjectForKey:@"key1"];
        NSLog(@"dic : %@",dic);
结果:

2014-08-26 20:17:33.980 Nsdictonary[695:707] dic : {

key1 = values1;

key2 = values2;

key3 = values3;

}

2013-08-26 20:17:34.013 Nsdictonary[695:707] dic : {

key2 = values2;

key3 = values3;

}

移除所有健值对:

removeAllObjects
        NSArray* values = [NSArray arrayWithObjects:@"values1",@"values2",@"values3", nil];
        NSArray* keys = [NSArray arrayWithObjects:@"key1",@"key2",@"key3", nil];
        NSMutableDictionary* dic2 = [NSMutableDictionary dictionaryWithObjects:values forKeys:keys];

        NSMutableDictionary* dic = [[NSMutableDictionary alloc] initWithDictionary:dic2];
        NSLog(@"dic : %@",dic);
        [dic removeAllObjects];
        NSLog(@"dic : %@",dic);
结果:

2013-08-26 20:18:38.027 Nsdictonary[711:707] dic : {

key1 = values1;

key2 = values2;

key3 = values3;

}

2013-08-26 20:18:38.036 Nsdictonary[711:707] dic : {

}

遍历字典:

for(id objects in dic)
        NSArray* values = [NSArray arrayWithObjects:@"values1",@"values2",@"values3", nil];
        NSArray* keys = [NSArray arrayWithObjects:@"key1",@"key2",@"key3", nil];
        NSMutableDictionary* dic2 = [NSMutableDictionary dictionaryWithObjects:values forKeys:keys];

        NSMutableDictionary* dic = [[NSMutableDictionary alloc] initWithDictionary:dic2];
        NSLog(@"dic : %@",dic);
        //类似于foreach
        for(id objects in dic)
        {
            NSObject* obj = [dic objectForKey:objects];
             NSLog(@"%@ = %@",objects,obj);
        }
结果:

2014-08-26 20:24:00.303 Nsdictonary[757:707] dic : {

key1 = values1;

key2 = values2;

key3 = values3;

}

2014-08-26 20:24:00.353 Nsdictonary[757:707] key1 = values1

2014-08-26 20:24:00.362 Nsdictonary[757:707] key3 = values3

2014-08-26 20:24:00.371 Nsdictonary[757:707] key2 = values2


迭代器遍历字典:

 NSEnumerator* em = [dic keyEnumerator];
        NSArray* values = [NSArray arrayWithObjects:@"values1",@"values2",@"values3", nil];
        NSArray* keys = [NSArray arrayWithObjects:@"key1",@"key2",@"key3", nil];
        NSMutableDictionary* dic2 = [NSMutableDictionary dictionaryWithObjects:values forKeys:keys];

        NSMutableDictionary* dic = [[NSMutableDictionary alloc] initWithDictionary:dic2];
        NSLog(@"dic : %@",dic);

        NSEnumerator* em = [dic keyEnumerator];
        id key =nil;
        while(key = [em nextObject])
        {
            NSObject* obj = [dic objectForKey:key];
            NSLog(@"%@ = %@",key,obj);
        }

结果:

2014-08-26 20:28:23.753 Nsdictonary[771:707] dic : {

key1 = values1;

key2 = values2;

key3 = values3;

}

2014-08-26 20:28:23.871 Nsdictonary[771:707] key1 = values1

2014-08-26 20:28:23.873 Nsdictonary[771:707] key3 = values3

2014-08-26 20:28:23.879 Nsdictonary[771:707] key2 = values2


block遍历字典:

[dic enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
            NSLog(@"%@ = %@",key,obj);
        }];
        NSArray* values = [NSArray arrayWithObjects:@"values1",@"values2",@"values3", nil];
        NSArray* keys = [NSArray arrayWithObjects:@"key1",@"key2",@"key3", nil];
        NSMutableDictionary* dic2 = [NSMutableDictionary dictionaryWithObjects:values forKeys:keys];

        NSMutableDictionary* dic = [[NSMutableDictionary alloc] initWithDictionary:dic2];
        NSLog(@"dic : %@",dic);

        [dic enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
            NSLog(@"%@ = %@",key,obj);
        }];
结果:

2014-08-26 20:32:09.894 Nsdictonary[789:707] dic : {

key1 = values1;

key2 = values2;

key3 = values3;

}

2014-08-26 20:32:09.906 Nsdictonary[789:707] key1 = values1

2014-08-26 20:32:09.917 Nsdictonary[789:707] key2 = values2

2014-08-26 20:32:09.913 Nsdictonary[789:707] key3 = values3

IOS中 NSDictionary无序字典排序

按NSDictionary的key来对其进行排序:

先将dict的allkeys赋给一个数组,然后通过sortedArrayUsingComparator:方法对数组排序,然后遍历数组取字典对应key的值就ok

NSArray *keys = [dict allKeys];

NSArray *sortedArray = [keys sortedArrayUsingComparator:^NSComparisonResult(idobj1, id obj2) {

return [obj1 compare:obj2 options:NSNumericSearch];

}];

for (NSString *categoryId in sortedArray) {

??

NSLog(@"[dict objectForKey:categoryId] === %@",[dictobjectForKey:categoryId]);

}

方法二:

NSString *filePath = [[NSBundle mainBundle]pathForResource:@"students" ofType:@"plist"];

NSDictionary *stuDic = [NSDictionary dictionaryWithContentsOfFile:filePath];

self.studentIndexArray = [NSMutableArray arrayWithCapacity:1];

NSArray *arr = [[stuDic allKeys] sortedArrayUsingSelector:@selector(compare:)];

时间: 2024-10-14 23:50:40

ios objective-c之-9:字典,可变字典 (NSDictonary NSMutableDictoray)的相关文章

OC中关于字典(可变字典)的使用---连载三

可变字典使用举例: 设计一个学生类Student, 有这些属性:name(姓名).age(年龄).score(分数).(classNum)班级 (1)将如下学生添加到数组中 姓名年龄分数班级 Tom1782Class01 Jim2275Class01 Jerry3454Class01 Owen2298Class04 Steve1977Class05 (2)计算所有学生的平均分(年级的平均分) (3)计算各个班级的平均分 (4)用名字作为key, value是学生对象,将这些学生存入字典. mai

Objective - C NSDictionary不可变字典和NSMutableDictionary可变字典

//创建一个不可变的字典对象 //便利构造器创建 NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:@"zhonger", @"name2", @"taixu", @"name1", @"caicai", @"name3", nil]; //初始化方法 //先给value 再给key NSDictionary *

利用runtime,避免UIButton 重复点击, 可变数组和可变字典为nil,或者数组越界导致的崩溃

Demo链接: https://github.com/ShaoWenLe/Runtimer-Demo.git 参考文章: http://www.jianshu.com/p/080a238c62b9 相关Runtime介绍: http://www.cocoachina.com/ios/20160523/16386.html http://www.cocoachina.com/ios/20160628/16843.html 1 #import <Foundation/Foundation.h> 2

OC基础 可变字典与不可变字典的使用

OC基础 可变字典与不可变字典的使用 1.不可变字典 1.1创建不可变字典 //创建字典 //注意: //1,元素个数是偶数 //2,每两个元素是一个键值对 //3,值在前,键在后 NSDictionary *dic = [[NSDictionary alloc] initWithObjectsAndKeys:@"huang",@"name",@"30",@"age", nil]; NSLog(@"%@",

objective-c可变字典

@import url(http://i.cnblogs.com/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/css/cuteeditor.css); 1 #pragma mark *****************************字典********************************  2 //        字典:通过key来取值的 key值必须是成对出现的,key不能为空(nil)  3 

Objective-C 字典、可变字典

字典相当于c++ stl中的map 字典NSDictionary 1 #import <UIKit/UIKit.h> 2 #import "AppDelegate.h" 3 4 int main(int argc, char * argv[]) { 5 // 从@符号创建字典 实际上就是键值对容器 6 NSDictionary *dict = @{@"name":@"zhangsan",@"sex":@"

字典与可变字典

字典的定义以及使用 不可变字典 1.在字典里 键 和 值是成对出现的    字典是通过键(Key)来存取值的且每一个值对应的Key是唯一的   字典的类名是NSDictionary 2.字典的定义  (1)初始化一个空字典 NSDictionary *dic = [[NSDictionary alloc] init]; NSDictionary *dic1 = [NSDictionary dictionary]; (2)初始化的同时添加一对键值  这种定义方式是前面是值(id)  后面是键 NS

不可变字典 赋值 求字典里有多少对 获取字典里所有value值 获取所有考值

#import <Foundation/Foundation.h> int main(int argc, const char * argv[]) { @autoreleasepool { /*字典:NSDictionary 存储数据用的   和数组的区别:字典使用键值方式.无序的 2 相当于我们使用字典通过目录来查找具体字的解释 3 用key-value的形式数据,value作用用来存储数据,key用来检索数据 4 cocoa中的字典:可变/不可变 注意: 1.key通常是字符串对象,可以是

OC中NSDictionary(字典)、NSMutableDictionary(可变字典)、NSSet(集合)、NSMutableSet(可变集合)得常用方法

字典用于保存具有映射关系数据的集合 一个key—value对认为是一个条目(entry),字典是存储key—value对的容器 与数组不同,字典靠key存取元素 key不能重复,value必须是对象 键值对在字典中是无序存储的 字典分:不可变字典(NSDictionary)和可变字典(NSMutableDictionary) 不可变字典一旦创建,键值对就不可更改,不可添加,不可删除,仅能读取key或者value 常用方法有: 1.创建字典对象 2.获取所有key值,获取所有value值 3.通过

iosOC不可变字典和可变字典

//key 和 value 都属于(id)对象类型 //key常用字符串NSString来表示 //存储数值型 一般可用 NSString //int  age ->@(age) //  [dic[name] intValue]; //   @()-->转成NSNumber 类型 //     intValue //value可以使任意对象(NSString  NSArrray , dog  ) //NSDictionary 一旦创建不能修改增加删除 #pragma mark 字典的创建 NS