[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 步骤 实现此案例需要按照如下步骤进行. ...
随机推荐
- LoadRunner安装时提示缺少C++ 2005 SP1(x86)插件
把安装文件里的所有中文文件重命名为英 文 名就ok!!! 把安装文件里的所有中文文件重命名为英 文 名就ok!!! 把安装文件里的所有中文文件重命名为英 文 名就ok!!! 重要的事情说三遍! 不插图 ...
- 组队训练 K K - The Stream of Corning 2
K - The Stream of Corning 2 这个题目不是很难,因为给你的这个S是单调递增的,所以就用优先队列+权值线段树就可以很快的解决了. 这个+读入挂可以优化,不过不用也没关系. #i ...
- WPF客户端自动升级
实现原理:通过一个辅助程序(更新程序.exe)比较本地版本号和服务器的版本,若服务器版本新则通过更新程序.exe下载服务器上资源(我是把最新的文件压缩成zip放到服务器上)到本地进行替换. 服务器放置 ...
- 【Hadoop离线基础总结】流量日志分析网站整体架构模块开发
目录 数据仓库设计 维度建模概述 维度建模的三种模式 本项目中数据仓库的设计 ETL开发 创建ODS层数据表 导入ODS层数据 生成ODS层明细宽表 统计分析开发 流量分析 受访分析 访客visit分 ...
- Excel+Word:Jupyter
直接打开Excel,可以增改删,但如果只是查了?Jupyter Lab/Jupyter Notebook是件利器. 工作内容之一,是复制Excel的一条记录,姓名.身份证号.银行卡号,粘贴在Word的 ...
- HMM-前向后向算法(附代码)
目录 基本要素 HMM三大问题 概率计算问题 前向算法 后向算法 前向-后向算法 基本要素 状态 \(N\)个 状态序列 \(S = s_1,s_2,...\) 观测序列 \(O=O_1,O_2,.. ...
- NetCore项目实战篇06---服务注册与发现之consul
至此,我们的解决方案中新建了三个项目,网关(Zhengwei.Gateway).认证中心(Zhengwei.Identity)和用户资源API(Zhengwei.Use.Api).当要访问用户API的 ...
- mfw
0x01 可能为git泄露 git泄露 githack下载源码 index.php <?php if (isset($_GET['page'])) { $page = $_GET['page'] ...
- PC、APP、H5三端测试的区别
一,针对同一个系统功能的测试,三端所测的业务流程是一样的 二,一般情况下手机端和PC端都对应一套后台服务,比如说笔者公司所开发的互联网金融平台,整个平台做了分布式服务架构,后台服务包括用户服务.交易服 ...
- Least Cost Bracket Sequence(贪心)
Least Cost Bracket Sequence(贪心) Describe This is yet another problem on regular bracket sequences. A ...