Foundation框架学习2--数组、字典、集合
2014-05-10 21:27
411 查看
1.数组
2.字典
3.集合
// 数组 // 数组的初始化 NSArray *firstArray=[NSArray arrayWithObject:@"one"]; // 多个元素初始化时 NSArray *secondArray=[NSArray arrayWithObjects:@"one",@"two",@"three", nil]; [secondArray writeToFile:@"/Users/lizhensheng/test1" atomically:YES]; NSArray *threeArray=[NSArray arrayWithArray:secondArray]; // 从文件中保存的xml 数据初始化数组 NSArray *fourthArray=[NSArray arrayWithContentsOfFile:@"/Users/lizhensheng/test1"]; // 遍历数组 // 遍历性能高 for (NSString *str in fourthArray) { NSLog(@"found %@",str); } // 数组中的常用方法 // 获取数组的元素个数 NSInteger count=[fourthArray count]; // 遍历性能低 for (int i=0; i<count; i++) { NSLog(@"for 遍历:%@",fourthArray[i]); } // 获取数组下标中的 值 NSString *astr1=[fourthArray objectAtIndex:0]; NSString *astr2=fourthArray[0]; // 在原来的数组上加上对象,并付给新的数组 NSArray *addArray=[fourthArray arrayByAddingObject:@"fine"]; // 用指定的字符,将数组中的元素连接起来 NSString *arrStr=[fourthArray componentsJoinedByString:@","]; // 判断数组中是否包含某对象 BOOL isContain=[fourthArray containsObject:@"one"]; //查询指定对象在数组中的元素,如果没有此元素,返回NSNotFound NSInteger index=[fourthArray indexOfObject:@"ccc"]; // 获取数组中最后一个元素 NSString *lastStr=[fourthArray lastObject]; // 可变数组 NSMutableArray *mutableArray=[NSMutableArray arrayWithCapacity:5]; // 向数组中添加元素 [mutableArray addObject:@"mutable1"]; // 向数组中添加数组 [mutableArray addObjectsFromArray:fourthArray]; // 向数组中指定下标插入元素 [mutableArray insertObject:@"bbb" atIndex:0]; // 移除最后一个元素 [mutableArray removeLastObject]; // 移除指定元素 [mutableArray removeObject:@"bbb"]; // 移除指定下标的元素 [mutableArray removeObjectAtIndex:0]; // 替换指定的下标元素 [mutableArray replaceObjectAtIndex:0 withObject:@"ccc"]; // 移除所有元素 [mutableArray removeAllObjects];
2.字典
// 字典 // 不可变字典 NSNumber *intNum=[NSNumber numberWithInt:100]; // 初始化一个元素 ,key value 必须是 对象 NSDictionary *dic1=[NSDictionary dictionaryWithObject:intNum forKey:@"key"]; // 初始化两个元素 NSDictionary *dic2=[NSDictionary dictionaryWithObjectsAndKeys:intNum,@"key1",intNum,@"key2", nil]; // 初始化字典包含其他字典 NSDictionary *dic3=[NSDictionary dictionaryWithDictionary:dic2]; // 把字典保存到文件中 [dic3 writeToFile:@"/Users/lizhensheng/test2" atomically:YES]; // 从文件中读取数据来初始化字典 NSDictionary *dic4=[NSDictionary dictionaryWithContentsOfFile:@"/Users/lizhensheng/test2"]; // 字典的常用方法 // 获取字典的 数量 NSInteger dicCount=[dic3 count]; // 通过key获取value NSString *value=[dic3 objectForKey:@"key"]; // 将字典的key转换成一个枚举对象,用于遍历 NSEnumerator *enumerator=[dic3 keyEnumerator]; // 获取所有键的集合 NSArray *keys=[dic3 allKeys]; // 获取所有值的集合 NSArray *values=[dic3 allValues]; // 可变字典 // 初始化一个可变字典 NSMutableDictionary *mdic1=[NSMutableDictionary dictionaryWithObjectsAndKeys:@"v1",@"k1",@"v2",@"k2", nil]; // 向字典中添加整个字典 [mdic1 addEntriesFromDictionary:dic3]; // 向字典中追加一个新的key和value [mdic1 setObject:@"v4" forKey:@"k4"]; // 初始化一个空的字典 NSMutableDictionary *mdic2=[NSMutableDictionary dictionary]; // 将空字典mdic2对象内容设置与字典2对象相同 [mdic2 setDictionary:dic3]; // 将字典中k1 对应的值删除 [mdic2 removeObjectForKey:@"k1"]; NSArray *dicArr=[NSArray arrayWithObjects:@"k1",@"k2", nil]; // 根据字典中的key 移除字典中的值 [mdic2 removeObjectsForKeys:dicArr]; // 移除所有的内容 [mdic2 removeAllObjects]; // 遍历字典 // 快速遍历 for (id key in mdic1) { id obj=[mdic1 objectForKey:key]; NSLog(@"快速遍历:key:%@ == value:%@",key,obj); } // 一般的枚举 NSArray *dkeys=[mdic1 allKeys]; NSUInteger dleng=[dkeys count]; for (int i=0; i<dleng; i++) { id key=[dkeys objectAtIndex:i]; id obj=[mdic1 objectForKey:key]; NSLog(@"一般的枚举:key:%@ == value:%@",key,obj); } // 通过枚举类型枚举 NSEnumerator *denumerator=[mdic1 keyEnumerator]; id dkey=[denumerator nextObject]; while (dkey) { id obj=[mdic1 objectForKey:dkey]; NSLog(@"枚举类型枚举:key:%@ == value:%@",dkey,obj); dkey=[denumerator nextObject]; }
3.集合
// 集合 // 不可变集合 // 集合的初始化 NSSet *set1=[[NSSet alloc]initWithObjects:@"set1",@"set2",@"set3", nil]; NSArray *setArr=[NSArray arrayWithObjects:@"1",@"2",@"3", nil]; // 通过数组构建集合 NSSet *set2=[NSSet setWithArray:setArr]; // 通过已经有的集合构建集合 NSSet *set3=[[NSSet alloc]initWithSet:set2]; // 常用方法 NSUInteger setCount =[set3 count]; //以数组的形式返回集合3的元素 NSArray *set3Arr=[set3 allObjects]; // 返回集合3中的任意一个对象 id setObj=[set3 anyObject]; // 判断集合中是否包含内容为3的对象,如果包含返回YES,否则返回NO BOOL isSetContain=[set3 containsObject:@"3"]; // 判断两个集合中是否有相同元素的对象,如果有返回YES,否则为NO BOOL isIntersect=[set2 intersectsSet:set1]; // 两个集合中元素是否完全匹配,如果匹配返回YES,否则返回NO BOOL isEqual=[set3 isEqualTo:set2]; // 集合3 是否是 集合2 的子集合,如果是返回YES,否则返回NO BOOL isSub=[set3 isSubsetOfSet:set2]; // 在原来集合的基础上添加新的对象,并付给新的集合 NSSet *set4=[set2 setByAddingObject:@"4"]; // 可变集合 NSMutableSet *mset1=[NSMutableSet set]; NSMutableSet *mset2=[NSMutableSet setWithObjects:@"1",@"2", nil]; NSMutableSet *mset3=[NSMutableSet setWithObjects:@"a",@"2", nil]; // 集合2 减去 集合3中的元素,集合2最后元素只有1个,且为1 [mset2 minusSet:mset3]; // 集合2 与集合3中元素的交集,集合2最后元素只有1个,且为2 [mset2 intersectsSet:mset3]; // 集合2 与集合3 的并集,集合2最后有3个,且为1,2,a [mset2 unionSet:mset3]; // 讲空集合1设为集合3的内容 [mset1 setSet:mset3]; for (id set in mset2) { NSLog(@"1set:%@",set); } NSArray *setArray=@[@"3"]; // 从数组中添加元素到集合中 [mset2 addObjectsFromArray:setArray]; for (id set in mset2) { NSLog(@"2set:%@",set); } // 根据内容删去集合中的对象 [mset2 removeObject:@"2"]; [mset2 removeAllObjects];
相关文章推荐
- Object-C Foundation 框架汇总之-数组,字典,集合
- 【IOS 开发学习总结-OC-31】★★OC之foundation 框架——字典(NSDictionary 与 NSMutableDictionary)
- IOS学习之Foundation框架—集合
- ObjC学习9-Foundation框架之数字、字符串和集合
- Foundation框架(3)数组对象及集合对象
- 黑马程序员——Foundation框架学习——OC结构体、字符串、OC集合、NSNumber,NSValue、OC时间对象
- NSLog字典和数组打印为中文(Foundation框架类目)
- Swift学习笔记第三篇(字符串,数组,集合,字典)
- 黑马程序员-OC学习笔记-----Foundation框架(集合、NSNumber、NSDate)
- OC中Foundation框架的数组和字典
- 黑马程序员-Foundation框架之数组和字典
- 【IOS 开发学习总结-OC-29】★★objective-c之foundation 框架——(集合类)数组
- Objective-C学习笔记_字典、集合、数组排序
- Foundation的基本操作—字符串、数组、字典、集合
- OC 集合-学习笔记(数组、字典、Set集合、数值数据)
- Python学习笔记3-Python元组、数组、字典集合的操作
- java学习日记_75:集合框架之对象数组和集合的引入
- swift基础学习(03)[数组、集合、字典]
- OC学习第五天_字典、集、数组排序:字典类、集合类、数组数组排序、字典、集合的快速遍历、数组排序
- Python学习笔记3-Python元组、数组、字典集合的操作