[Objective-C] 010_Foundation框架之NSSet与NSMutableSet
在Cocoa Foundation中的NSSet和NSMutableSet ,和NSArray功能性质一样,用于存储对象属于集合。但是NSSet和NSMutableSet是无序的, 保证数据的唯一性,当插入相同的数据时,不会有任何效果。
NSSet 初始化及常用操作
#import "AppDelegate.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
NSSet *students = [NSSet setWithObjects:@"小明", @"小辉", @"大雄", nil];
NSSet *teachers = [[NSSet alloc] initWithObjects:@"校长", @"副校长", @"政教主任", nil];
NSArray *array = [NSArray arrayWithObjects:@"小明", @"小辉", @"大雄",@"小李", nil];
NSSet *students_2 = [NSSet setWithArray:array];
NSLog(@"students :%@", students);
NSLog(@"teachers :%@", teachers);
NSLog(@"students_2 :%@", students_2);
//获取集合students包含对象的个数
NSLog(@"students count :%lu", (unsigned long)students.count);
//以数组的形式获取集合teachers中的所有对象
NSArray *allTeacher = [teachers allObjects];
NSLog(@"allObj :%@", allTeacher);
//获取teachers中任意一对象
NSLog(@"anyObj :%@", [teachers anyObject]);
//teachers是否包含某个对象
if ([teachers containsObject:@"副校长"]) {
NSLog(@"teachers中有副校长");
}
//是否包含指定set中的对象
if ([students_2 intersectsSet:students]) {
NSLog(@"intersects");
}
//是否完全匹配
if ([students_2 isEqualToSet:students]) {
NSLog(@"完全匹配");
}else{
NSLog(@"完全匹配? NO。。。。。。。");
}
//是否是子集合
if ([students isSubsetOfSet:students_2]) {
NSLog(@"students isSubsetOf students_2");
}
//迭代器遍历
NSEnumerator *enumerator = [teachers objectEnumerator];
NSObject *teacher = [enumerator nextObject];
while (teacher != nil) {
NSLog(@"teachers中的数据: %@",teacher);
teacher = [enumerator nextObject];
}
//快速枚举遍历
for (NSObject *teacher in teachers) {
NSLog(@"teachers中的数据: %@",teacher);
}
return YES;
}
@end
NSMutableSet 初始化及常用操作
#import "AppDelegate.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
NSMutableSet *mutableStudent = [NSMutableSet setWithObjects:@"F1", @"F2", @"F3", nil];
NSMutableSet *mutableTeacher = [NSMutableSet setWithObjects:@"B1", @"B2", @"B3", nil];
NSMutableSet *mutableStudent2 = [NSMutableSet setWithObjects:@"F1", @"F2", @"F3",@"F4", nil];
//集合元素相减
[mutableStudent2 minusSet:mutableStudent];
NSLog(@"mutableStudent2 minus mutableStudent:%@", mutableStudent2);
//mutableStudent2只留下相等元素
[mutableStudent intersectSet:mutableStudent2];
NSLog(@"intersect :%@", mutableStudent2);
//mutableStudent合并集合
[mutableStudent unionSet:mutableStudent2];
NSLog(@"union :%@", mutableStudent);
//mutableTeacher删除指定元素
[mutableTeacher removeObject:@"好色仙人"];
NSLog(@"removeObj :%@", mutableTeacher);
//mutableTeacher删除所有数据
[mutableTeacher removeAllObjects];
NSLog(@"removeAll :%@", mutableTeacher);
return YES;
}
@end

本站文章为宝宝巴士 SD.Team原创,转载务必在明显处注明:(作者官方网站:宝宝巴士)
转载自【宝宝巴士SuperDo团队】 原文链接: http://www.cnblogs.com/superdo/p/4623082.html
[Objective-C] 010_Foundation框架之NSSet与NSMutableSet的更多相关文章
- NSSet、NSMutableSet基本用法
NSSet.NSMutableSet基本用法 在Foundation框架中,提供了NSSet类,它是一组单值对象的集合,且NSSet实例中元素是无序,同一个对象只能保存一个. 一.不可变集合NSSet ...
- NSSet、NSMutableSet
NSSet和NSArray功能性质一样,用于存储对象,属于集合:只能添加cocoa对象,基本数据类型需要装箱. NSSet . NSMutableSet是无序的集合,在内存中存储方式是不连续的,而NS ...
- NSSet和NSMutableSet 确保数据的唯一性--备
NSSet和NSMutableSet是无序的, 但是它保证数据的唯一性.当插入相同的数据时,不会有任何效果.从内部实现来说是hash表,所以可以常数时间内查找一个数据. 1.NSSet的使用 [NSS ...
- Objective - c Foundation 框架详解2
Objective - c Foundation 框架详解2 Collection Agency Cocoa provides a number of collection classes such ...
- IOS集合NSSet与NSMutableSet知识点
NSSet在实际应用中与NSArray区别不大,但是如果你希望查找NSArray中的某一个元素,则需要遍历整个数组,效率低下.而NSSet在查找某一特定的元素的时候则是根据hash算法直接找到此元素的 ...
- [OC Foundation框架 - 21] NSSet集合 & 集合之间的转换
A.NSSet 跟NSArray一样,不可变 NSArray 自然顺序 NSSet是无序的 NSSet不允许存入重复元素,可以用来过滤重复元素 也有可变的NSMutableSet B.集合转换 ...
- Object-C学习之NSSet和NSMutableSet
转自:http://blog.csdn.net/likandmydeer/article/details/7939749 一.简介 集合(set)是一组单值对象,它可以是固定的(NSSet).也可以是 ...
- NSSet和NSMutableSet - By吴帮雷
1.NSSet的使用 [NSSet setWithSet:(NSSet *)set]; 用另外一个set对象构造 [NSSet setWithArray:(NSArray *)array];用数组构造 ...
- NSArray(二) 、 NSMutableArray 、 NSSet 、 NSMutableSet
1 创建五个学生对象,放入数组并遍历 1.1 问题 创建一个自定义类TRStudent,为该类生成五个对象.把这五个对象存入一个数组当中,然后遍历数组. 1.2 步骤 实现此案例需要按照如下步骤进行. ...
随机推荐
- MySQL 增删改查(单表)
1.sql 新增语句 表中插入数据 insert into + 表名 values(字段1value1,字段2value1,字段3value1),(字段1value2,字段2value2,字段3val ...
- Fiddler手机端抓包环境设置与过滤(一)
一.PC端Fiddler设置 1.安装https 证书 打开Fiddler->Tool->Fiddler Options->HTTPS tab,勾选上并Capture HTTPS C ...
- search(9)- elastic4s logback-appender
前面写了个cassandra-appender,一个基于cassandra的logback插件.正是cassandra的分布式数据库属性才合适作为akka-cluster-sharding分布式应用的 ...
- CSS躬行记(9)——网格布局
网格布局(Grid Layout)也叫栅格布局,与表格布局类似,也依赖行和列.但与之不同的是,网格布局能直接控制HTML文档中元素的顺序.位置和大小等,而不用再借助辅助元素. 一.术语 下图展示了CS ...
- 第五章:深入Python的dict和set
第五章:深入Python的dict和set 课程:Python3高级核心技术 5.1 dict的abc继承关系 class Mapping(Collection): __slots__ = () &q ...
- Qt之分模块log
说明 对于一般的log,使用 qInstallMessageHandler 重定向到文件即可,甚至可以根据日志等级,分类存储.但是并不是适用所有情况,比如,程序运行时动态创建模块,而每个模块需要创建不 ...
- spark on yarn安装
网上关于spark的安装说明很多了,这里就以spark pre-build with user provided hadoop 安装包为例讲解, 下载spark pre-build with us ...
- 数组的操作。1,JS数组去重。2,把数组中存在的某个值,全部找出来。3在JS数组指定位置插入元素。。。
1,数组去重 let arr = [1,2,3,4,5,6,1,2,3,'a','b','a']; let temp = []; // 作为存储新数组使用 for(let i = 0; i < ...
- 盘点6个Kubernetes监视工具
导读:监控可帮助您确保Kubernetes应用程序平稳运行并排除可能出现的任何问题.Prometheus是一种流行的开源监视工具,许多公司都使用它来监视其IT基础结构.但是,还有许多其他监视工具可用. ...
- 笔记二(JavaWeb)
上一个笔记写的好累,这次换Markdown试试 缺省适配器设计模式:父类不实现该方法,让子类去实现(抽象方法) 模板方法设计模式:定义一个操作中的方法骨架,而将一些步骤延迟到子类中.模板方法使得子类可 ...