iOS阶段学习第15天笔记(NSDictionary与NSMutableDictionary 字典)

时间:2023-03-08 23:12:42
iOS阶段学习第15天笔记(NSDictionary与NSMutableDictionary 字典)

iOS学习(OC语言)知识点整理

一、OC中的字典

1)字典:是一个容器对象,元素是以键-值对(key-value)形式存放的,key和value是任意类型的对象,key是唯一的,value可以重复

2)OC中的字典有两种

1、不可变字典:NSDictionary,初始化后不能修改其内容

2、可变字典:NSMutableDictionary,初始化后可以修改其内容

二、NSDictionary 字典的操作

1)实例化方法创建字典对象 例如:

 NSDictionary *dict1=[[NSDictionary alloc]initWithObjectsAndKeys:@"one",@"",@"two",@"",@"three",@"",@"one11",@"",@"two",@"", nil];
//注:前一个是值(value)后一个是键(key)

2)count  可计算字典中key-value的个数 例如:  NSLog(@"%ld",[dict1 count]);

3)initWithDictionary 用一个字典创建另一个字典对象 例如:
       NSDictionary *dict2=[[NSDictionary alloc]initWithDictionary:dict1];

4)dictionaryWithObjectsAndKeys 用类方法创建对象 例如:
       NSDictionary *dict3=[NSDictionary dictionaryWithObjectsAndKeys:<#(id), ...#>, nil];

5)快速创建对象@{key1:value1,key2:value2,....} 例如:

 NSDictionary *dict3=@{@"apple":@"苹果",@"red":@"红色"};
//注:前面是键(key) 后面是值(value)有点类似于Json数据类型

6)objectForKey 用于根据key取value 例如:
       NSString*value=[dict1 objectForKey:@“”]; //结果:two

7)allKeysForObject 根据value取出对应的所有的key(value可以重复,key不能重复)例如:
       NSArray *keys=[dict1 allKeysForObject:@"two"];

8)allKeys 用于取出所有的key  例如:
       NSArray *allKeys=[dict1 allKeys];

9)allValues 用于取出所有的value 例如:
       NSArray *allValues=[dict1 allValues];

10)NSDictionary 遍历方法

1、迭代器法:

 NSEnumerator *enumerator=[dict1 keyEnumerator];
id obj;
//[enumerator nextObject]:如果有key,就返回,否则为nil,自动指向下一个key
while (obj=[enumerator nextObject]) {
NSLog(@"%@-->%@",obj,[dict1 objectForKey:obj]);
}

2、快速遍历法:

  for(id key in dict1){
NSLog(@"%@*******%@",key,[dict1 objectForKey:key]);
}

三、NSMutableDictionary字典的操作

1)NSMutableDictionary继承自NSDictionary。

2) 创建一个空的字典对象 例如:  NSMutableDictionary *dict1=[[NSMutableDictionary alloc]init];

3)setObject…  forKey… 如果key不存在,就添加,如果key存在,就修改 例如:
       [dict1 setObject:@"one" forKey:@""];

4)addEntriesFromDictionary 将另一个字典的内容全部添加过来 例如:

 NSDictionary *subDict=@{@"":@"two",@"":@"three",@"":@"one1",@"":@"four"};
[dict1 addEntriesFromDictionary:subDict];

5)removeObjectForKey 根据key删除元素(key和对应的value都将被删除)例如:
      dict1 removeObjectForKey:@""];

6)removeObjectsForKeys 用于删除多个key及对应的value  例如:
       NSArray *keys=@[@"",@""]; [dict1 removeObjectsForKeys:keys];

7)removeAllObjects 删除字典中所有的元素 例如:
      [dict1 removeAllObjects];

8)setDictionary 用另一个字典重新设置该字典的内容 例如:
      [dict1 setDictionary:subDict];

四、OC中的打包与解包

1)打包即将一个值类型数据转换为对象类型数据的过程 例如:

  //将int型的数据封装成对象(打包)
NSNumber *intNumber=[[NSNumber alloc]initWithInt:];
NSNumber *longNumber=[NSNumber numberWithInteger:];
NSNumber *charNumber=[[NSNumber alloc]initWithChar:'A'];

2) NSNumber:用于将数据封装成对象。

3)解包即将一个对象类型转换为值类型的一个过程  例如:

 //取出对象中的基本数据值(解包)
NSLog(@"%d",[intNumber intValue]);
NSLog(@"%ld",[longNumber integerValue]);
NSLog(@"%c",[charNumber charValue]);

4)compare 用于比较两个对象的数据值的大小

实例代码:

 NSComparisonResult cmp= [intNumber compare:longNumber];
//NSNumber可以直接显示数据值,已经重写过description方法
if(cmp==NSOrderedAscending){
NSLog(@"%@<%@",intNumber,longNumber);
}else if (cmp==NSOrderedDescending){
NSLog(@"%@>%@",intNumber,longNumber);
}else if (cmp==NSOrderedSame){
NSLog(@"%@=%@",intNumber,longNumber);
}

5)NSValue:将结构体和指针类型的数据封装成对象

6)将结构体数据封装成对象 。 注:不能将结构体变量存入数组,需要将其封装为NSValue的对象

实例代码:

  struct mysct
{
int a;
int b;
};
struct mysct s1={,},s2;
NSValue *value1=[[NSValue alloc]initWithBytes:&s1 objCType:@encode(struct mysct)];
NSLog(@"%s,%s",@encode(struct mysct),@encode(int));//结果:{mysct=ii},i
NSArray *array1=[[NSArray alloc]initWithObjects:value1, nil];
//将结构体变量封装的对象存入数组
NSArray *array1=[[NSArray alloc]initWithObjects:value1, nil];
NSValue *value2=[array1 firstObject];
//将value2中的数据取出存入s2
[value2 getValue:&s2];
NSLog(@"s2:{%d,%d}",s2.a,s2.b);//结果:1,2

7)@encode(aType) 可以返回该类型的 C 字符串(char *)的表示。

五、OC中随机数生成

1、rand 用于生成int类型的随机数 ;rand()实际并不是一个真正的伪随机数发生器,random()会相对好点。

2、random 用于生成long类型的随机数;需要初始化时设置种子 例如:
            srandom((unsigned int)time(time_t *)NULL); //初始化时,设置下种子就好了。

3、arc4random 用于生成unsigned int 类型的随机数据 例如:

 int value = arc4random() % x; //表示产生0到x-1之间的整数随机数
int value = (arc4random() % x) + ; //表示产生1到x之间的整数随机数

六、OC中常用的结构体

1)OC中常用的结构体 NSPoint,NSRect,NSSize,NSRange

1、 NSPoint 用于获取/设置 对象的坐标点 实例代码:

 NSPoint pt=NSMakePoint(, );
NSValue *vl1=[NSValue valueWithPoint:pt];
NSPoint pt2=[vl1 pointValue];
NSLog(@"Point=(%.0f,%.0f)",pt2.x,pt2.y);//结果:Point=(10,20)

2、NSRect 用于获取/设置 对象的坐标点以及长度和宽度 实例代码:

 NSRect rt=NSMakeRect(, , , );
NSValue *vl3=[NSValue valueWithRect:rt];
NSRect pt4=[vl3 rectValue];
NSLog(@"Rect=(%.0f,%.0f,%.0f,%.0f)",pt4.origin.x,pt4.origin.y,pt4.size.width,pt4.size.height);//结果:Rect=(2,6,20,10)

3、NSSize  用于获取/设置 对象的长度和宽度 实例代码:

 NSSize rs=NSMakeSize(, );
NSValue *vl4=[NSValue valueWithSize:rs];
NSSize pt5=[vl4 sizeValue];
NSLog(@"Size=(%.0f,%.0f)",pt5.width,pt5.height);

4、NSRange 用于获取/设置 对象的起始坐标和长度  实例代码:

 NSRange rg=NSMakeRange(, );
NSValue *vl2=[NSValue valueWithRange:rg];
NSRange pt3=[vl2 rangeValue];
NSLog(@"Range=(%lu,%lu)",pt3.location,pt3.length);

2)基本类型数据只有转变成对象才能操作里面的元素。