iOS Collection 集合
来源:互联网 发布:熊猫智能电视软件下载 编辑:程序博客网 时间:2024/06/15 21:02
1、NSNumber
// NSNumber numberWithChar NSNumber * num1 = [NSNumber numberWithChar:'A']; NSLog(@"num1=%@",num1); // numberWithInt // numberWithFloat // numberWithBool num1 = [NSNumber numberWithBool:NO]; NSLog(@"num1=%@",num1); // charVaule // intValue // floatValue // boolValue NSLog(@"%hhd",[num1 boolValue]);
上面Demo的Log输出为:
2015-06-22 09:49:37.591 ObjC03[5534:303] num1=65
2015-06-22 09:49:37.595 ObjC03[5534:303] num1=0
2015-06-22 09:49:37.596 ObjC03[5534:303] 0
// NSValue CGSize si; si.height = 20; si.width = 10; // char a[2] = {'a','b'}; NSValue * value = [NSValue valueWithBytes:&si objCType:@encode(CGSize)]; // valueWithPoint // valueWithSize // valueWithRect CGSize size =[value sizeValue]; NSLog(@"width=%f,height=%f",size.width,size.height);上面代码的Log输出为:
2015-06-22 09:49:37.596 ObjC03[5534:303] width=10.000000,height=20.000000
3、Collection集合
Collection(集合)是能够容纳多个事物的容器,在iOS中的集合有数组,字典,集合;
数组: 可以包含相同的元素
- NSArray:不可变数组,可以存放多个对象,实例化的时候以nil表示结束,通过下标索引对象,NSArray中无法添加,删除,修改元素;
- NSMutableArray:可变数组,是NSArray的子类,相比NSArray增加了添加,删除,修改元素的功能;
字典:
- NSDictionary:不可变字典,存放多个键值对(key-value),最后以nil结束,字典中的对象存储没有顺序,只能使用key来索引每个对象,不可以添加,删除,修改元素;
- NSMutableDictionary:可变字典,是NSDirctionary的子类;
在实例化不可变字典对象时,如果在参数的前边已经包含了某一个关键字key,那么在后边出现的相同的关键字则不会添加到字典内;
在对可变字典对象添加元素时,如果在字典对象内已经包含了对应的key,则会用新的value值替换之前的value值;
集合: 不能包含相同的元素
- NSSet:不可变集合,可以存放多个对象,以nil表示结束,对象在set中是无序的,不可以添加,删除,修改元素;
- NSMutableSet:可变集合,是NSSet的子类;
可变类通常比不可变类多了增删改的功能;
Set集合是无序的,随机取数据;
Collection的内存管理:
Collection会对自己内部的元素负责,元素添加(含初始化)到Collection的时候,元素会自动retain,当元素remove的时候,元素会自动release,当Collection释放销毁的时候,会先让内部元素release,然后自己再release;
4、NSArray和NSMutableArray
NSMutableArray是NSArray的子类,NSArray长度固定,而NSMutableArray长度可变;
// NSArray NSArray *arr = [[NSArray alloc] initWithObjects: @"zero", @"one", nil]; // count NSLog(@"arr.count=%lu",[arr count]); // objectAtIndex NSLog(@"arr at 1 = %@",[arr objectAtIndex:1]); NSString *str = [NSString stringWithFormat:@"ten"]; // [arr insertValue:<#(id)#> inPropertyWithKey:<#(NSString *)#>]; // NSMutableArray NSMutableArray *arr1 = [[NSMutableArray alloc]initWithCapacity:12]; // arr1 = [NSMutableArray arrayWithCapacity:12]; // addObject [arr1 addObject:@"two"]; NSLog(@"arr1.count=%lu",[arr1 count]); NSLog(@"arr1 at 0 = %@",[arr1 objectAtIndex:0]); [arr1 addObject:str]; NSLog(@"arr1.count=%lu",[arr1 count]); NSLog(@"arr1 at 1 = %@",[arr1 objectAtIndex:1]); // removeAllObjects // removeLastObject // removeObjectAtIndex // replaceObjectAtIndex [arr1 replaceObjectAtIndex:1 withObject:@"three"]; NSLog(@"arr1 at 1 = %@",[arr1 objectAtIndex:1]);上面代码的Log输出为:
2015-06-22 09:49:37.597 ObjC03[5534:303] arr.count=2
2015-06-22 09:49:37.598 ObjC03[5534:303] arr at 1 = one
2015-06-22 09:49:37.599 ObjC03[5534:303] arr1.count=1
2015-06-22 09:49:37.603 ObjC03[5534:303] arr1 at 0 = two
2015-06-22 09:49:37.605 ObjC03[5534:303] arr1.count=2
2015-06-22 09:49:37.612 ObjC03[5534:303] arr1 at 1 = ten
2015-06-22 09:49:37.613 ObjC03[5534:303] arr1 at 1 = three
5、NSDictionary和NSMutableDictionary
字典中 key 一般用 NSString 或者 NSNumber,而且值可以是任一类型;
值对象不能为nil;
// NSDictionary // -(id)initWithObjects :(NSArray *) objectforKeys: (NSArray *)keys // +(id)dictionaryWithObject :id(anObject) // [NSDictionary dictionaryWithObject:<#(id)#> forKey:<#(id<NSCopying>)#>]; NSDictionary * dic = [[NSDictionary alloc] initWithObjectsAndKeys:@"value1",@"key1",@"value2",@"key2", nil]; // count NSLog(@"dic.count = %lu",[dic count]); // get allKeys NSArray *arrKeys =[dic allKeys]; int count = (int)[arrKeys count]; // loop1 for(int i = 0;i<count ;i++){ NSLog(@"dic.key%d = %@",(i + 1),[arrKeys objectAtIndex:i]); // 1.getValue from key NSLog(@"dic.value = %@",[dic objectForKey:[arrKeys objectAtIndex:i]]); // 2.getValue from key // NSLog(@"dic.value = %@",[dic valueForKey:[arrKeys objectAtIndex:i]]); } // loop2 NSString * key; for(k in dic){ NSLog(@"key = %@ , value=%@", key , [dic valueForKey:key]); } // NSMutableDictionary NSMutableDictionary *dic2 = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"v1",@"k1",@"v2",@"k2", nil]; [dic2 setObject:@"v3" forKey:@"k3"]; NSLog(@"dic2.length = %d",(int)[dic2 count]); NSLog(@"dic2's value of k3 = %@",[dic2 objectForKey:@"k3"]); // setValue [dic2 setValue:@"k3_new" forKey:@"k3"]; NSLog(@"dic2's value of k3 = %@",[dic2 objectForKey:@"k3"]); // removeObjectForKey :(id)key; // removeAllObjects;上面代码的输出为:
2015-06-22 09:59:53.624 ObjC04[5574:303] dic.count = 2
2015-06-22 09:59:53.628 ObjC04[5574:303] dic.key1 = key1
2015-06-22 09:59:53.629 ObjC04[5574:303] dic.value = value1
2015-06-22 09:59:53.630 ObjC04[5574:303] dic.key2 = key2
2015-06-22 09:59:53.632 ObjC04[5574:303] dic.value = value2
2015-06-22 09:59:53.634 ObjC04[5574:303] key = key1 , value=value1
2015-06-22 09:59:53.635 ObjC04[5574:303] key = key2 , value=value2
2015-06-22 09:59:53.636 ObjC04[5574:303] dic2.length = 3
2015-06-22 09:59:53.637 ObjC04[5574:303] dic2's value of k3 = v3
2015-06-22 09:59:53.638 ObjC04[5574:303] dic2's value of k3 = k3_new
6、NSSet和NSMutableSet
NSSet 集合和数组差不多,不过集合中不能存放相同的对象,是一组单值对象的集合,并且被存放到集中的元素是无序的;
// NSSet NSSet * set = [NSSet setWithObjects:@"zero", @"one", @"three", nil]; // -(id)initWithObjects NSLog(@"set's count = %d",(int)[set count]); // allObjects count = (int)[set count]; NSArray *objs = [set allObjects]; // loop1 for(int i = 0;i<count ;i++){ NSLog(@"%@",[objs objectAtIndex:i]); } // loop2 for(NSString * e in objs){ NSLog(@"e = %@",e); } // anyObject NSString * obj = [set anyObject]; NSLog(@"anyObject=%@",obj); // isEqualToSet : compare two set // member : obj (contains) NSLog(@"is contains 'one' =%@",[set member:@"one"]); NSLog(@"is contains 'two' =%@",[set member:@"two"]); // NSMutableSet // 指定新分配的集合为size个成员存储空间 NSMutableSet * set1 = [[NSMutableSet alloc] initWithCapacity:12]; [set1 addObject:@"zero_0"]; NSString * any1 = [set1 anyObject]; NSLog(@"any1 = %@",any1);以上代码的Log输了为:
2015-06-22 09:59:53.639 ObjC04[5574:303] set's count = 3
2015-06-22 09:59:53.641 ObjC04[5574:303] zero
2015-06-22 09:59:53.642 ObjC04[5574:303] one
2015-06-22 09:59:53.643 ObjC04[5574:303] three
2015-06-22 09:59:53.643 ObjC04[5574:303] e = zero
2015-06-22 09:59:53.644 ObjC04[5574:303] e = one
2015-06-22 09:59:53.645 ObjC04[5574:303] e = three
2015-06-22 09:59:53.647 ObjC04[5574:303] anyObject=zero
2015-06-22 09:59:53.649 ObjC04[5574:303] is contains 'one' =one
2015-06-22 09:59:53.651 ObjC04[5574:303] is contains 'two' =(null)
2015-06-22 09:59:54.376 ObjC04[5574:303] any1 = zero_0
7、快速枚举
枚举:对集合中的元素依次,不重复地一一进行遍历。
传统语法中,枚举一般通过 for 循环来进行:
for(int i = 0;i < n;i++){<span style="white-space:pre"></span>id anObject = [array objectAtIndex:i];}
Objective-C 2.0 中提供了一种简洁的特色语法,令你可以安全又快速地对集合中的元素进行枚举。
for...in 是 Objective-C 2.0 中的特色语法,它的标准形式如下:
for(Type newVariable in expression){<span style="white-space:pre"></span>statements;}
或者:
Type variable;for(variable in expression){<span style="white-space:pre"></span>statements;}
遍历:
[dic enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) { NSLog(@"%@=%@,end = %d",key, obj, *stop); }]; [arr enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { NSLog(@"arr:%@ %d",obj, *stop); }];
- iOS Collection 集合
- 集合Collection
- 集合collection
- Collection集合
- Collection集合
- Collection集合
- 集合 Collection
- Collection集合
- Collection集合
- 集合Collection
- 集合collection
- Collection集合
- 集合Collection
- 集合(Collection)
- Collection集合:
- 集合 collection
- 集合Collection
- Collection集合
- POJ 1493 Machined Surfaces(水~)
- 关于精简点名APP的使用说明及使用
- tomcat、servlet、jsp的一个典型例子
- Http抓包工具--查尔斯
- 抽象类和接口的区别
- iOS Collection 集合
- 为什么要引入原语???
- Android中的消息处理实例与分析
- 在Linux中调度功能是由哪个函数来实现的???
- 进程管理与SELinux初探
- 《Artech的WCF之旅系列》系列技术文章整理收藏
- 仿QQ软件收获
- 关于版本管理的一些初步理解
- 进程与PCB的关系如何看???