Objective-C----NSSet 、 NSMutableSet 、 NSCountedSet
来源:互联网 发布:mysql教程合集 编辑:程序博客网 时间:2024/04/29 19:32
直接上代码:
/* * NSSet 不可变 集合 * */ // 两种初始化方式 NSSet *set1 = [[NSSet alloc] initWithObjects:@"1", @"2", @"3", nil] ; NSLog( @"%@", set1 ) ; NSSet *set2 = [NSSet setWithObjects:@"12", @"23", @"34", nil] ; NSLog( @"%@", set2 ) ; //用数组对象来创建集合对象 NSArray *array = @[@1, @2, @2] ; //initWithArray 和 setWithArray 将数组对象转换成集合对象,这样能将数组中重复的对象过滤掉 NSSet *set3 = [[NSSet alloc] initWithArray:array] ; NSLog( @"%@", set3 ) ; NSSet *set4 = [NSSet setWithArray:array] ; NSLog( @"%@", set4 ) ; //获取集合中对象的个数 NSLog( @"%ld", [set4 count] ) ; //获取集合中的对象(返回的是任意一个对象,如果集合中没有对象,则返回nil) id object1 = [set4 anyObject] ; NSLog( @"%@", object1 ) ; //判断一个给定的对象是否包含在指定的集合中 NSString *result1 = [set4 containsObject:@2] ? @"YES" : @"NO" ; NSLog( @"%@ is contained int set %@", @2, result1 ) ;// @2 换成 @"2" 结果打印的是 NO// NSString *result1 = [set4 containsObject:@"2"] ? @"YES" : @"NO" ;// NSLog( @"%@ is contained int set %@", @"2", result1 ) ; /* * NSMutableSet 可变 集合 * */ //初始化 NSMutableSet *mutableSet1 = [[NSMutableSet alloc] init] ; NSLog( @"%@", mutableSet1 ) ; NSMutableSet *mutableSet2 = [NSMutableSet set] ; NSLog( @"%@", mutableSet2 ) ; //通过不可变对象创建 NSMutableSet *mutableSet3 = [[NSMutableSet alloc] initWithSet:set1] ; NSLog( @"%@", mutableSet3 ) ; NSMutableSet *mutableSet4 = [NSMutableSet setWithSet: set1] ; NSLog( @"%@", mutableSet4 ) ; //添加集合元素(注意:@4 和 @"4"不一样) [mutableSet4 addObject:@4] ; NSLog( @"%@", mutableSet4 ) ; //删除单个集合元素 [mutableSet4 removeObject:@4] ; NSLog( @"%@", mutableSet4 ) ; //删除所有集合元素 [mutableSet4 removeAllObjects] ; NSLog( @"%@", mutableSet4 ) ; /* * NSCountedSet * * 是 NSSet的子类,能记录集合中的元素的重复次数 */ // NSCountedSet *countSet1 = [NSCountedSet set] ; [countSet1 addObject:@1] ; [countSet1 addObject:@2] ; [countSet1 addObject:@3] ; [countSet1 addObject:@2] ; NSLog( @"%@", countSet1 ) ; //单独获取某个对象在集合中出现过多少次// NSLog( @"%ld", [countSet1 countOfObjc:@3] ) ; NSLog( @"%ld", [countSet1 countForObject:@5] ) ;
1 0
- Objective-C----NSSet 、 NSMutableSet 、 NSCountedSet
- Objective-C语法之NSSet和NSMutableSet
- Objective-c 集合对象 (NSSet 和 NSMutableSet)
- Objective-C语法之NSSet和NSMutableSet
- Objective-C语法之NSSet和NSMutableSet
- Objective-C语法之NSSet和NSMutableSet
- Objective-C语法之NSSet和NSMutableSet
- Objective-C语法之NSSet和NSMutableSet
- Objective-C语法之NSSet和NSMutableSet
- Objective-C基础—— NSSet & NSMutableSet
- Objective-C语法之NSSet和NSMutableSet
- Objective-C: Foundation——NSSet、NSMutableSet
- 21 Foundation框架之NSSet、NSMutableSet、NSCountedSet、NSOrderedSet、NSMutableOrderedSet
- 10,Objective-C语法之NSSet和NSMutableSet
- 12、Objective-C语法之NSSet和NSMutableSet
- OC学习之道:OC对象,字典,集合的使用方法NSDictionary,NSMutableDictionary, NSSet,NSMutableSet,NSCountedSet
- Objective-c的Foundation中常用类5——集合中的NSSet、NSMutableSet
- 黑马程序员——Objective-C学习之无序集合(NSSet\NSMutableSet)
- To_10_r_100_8_1---判断出三盏灯分别是由哪个开关控制的
- css清除浮动
- JAVA中转义字符
- spring+mybatis 多数据源整合
- windows xp 恢复删除的分区
- Objective-C----NSSet 、 NSMutableSet 、 NSCountedSet
- Effective C++ 条款3
- Android数据库高手秘籍(一)——SQLite命令
- 天声人語 20150620
- Objective-C----快速枚举、数组排序
- 袖珍实干的卸载小软Soft Organizer,使用和测评:
- Android数据库高手秘籍(二)——创建表和LitePal的基本用法
- SQL 标量函数&表值函数
- static用法