在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的更多相关文章

  1. NSSet、NSMutableSet基本用法

    NSSet.NSMutableSet基本用法 在Foundation框架中,提供了NSSet类,它是一组单值对象的集合,且NSSet实例中元素是无序,同一个对象只能保存一个. 一.不可变集合NSSet ...

  2. NSSet、NSMutableSet

    NSSet和NSArray功能性质一样,用于存储对象,属于集合:只能添加cocoa对象,基本数据类型需要装箱. NSSet . NSMutableSet是无序的集合,在内存中存储方式是不连续的,而NS ...

  3. NSSet和NSMutableSet 确保数据的唯一性--备

    NSSet和NSMutableSet是无序的, 但是它保证数据的唯一性.当插入相同的数据时,不会有任何效果.从内部实现来说是hash表,所以可以常数时间内查找一个数据. 1.NSSet的使用 [NSS ...

  4. Objective - c Foundation 框架详解2

    Objective - c  Foundation 框架详解2 Collection Agency Cocoa provides a number of collection classes such ...

  5. IOS集合NSSet与NSMutableSet知识点

    NSSet在实际应用中与NSArray区别不大,但是如果你希望查找NSArray中的某一个元素,则需要遍历整个数组,效率低下.而NSSet在查找某一特定的元素的时候则是根据hash算法直接找到此元素的 ...

  6. [OC Foundation框架 - 21] NSSet集合 & 集合之间的转换

    A.NSSet 跟NSArray一样,不可变 NSArray 自然顺序 NSSet是无序的 NSSet不允许存入重复元素,可以用来过滤重复元素   也有可变的NSMutableSet   B.集合转换 ...

  7. Object-C学习之NSSet和NSMutableSet

    转自:http://blog.csdn.net/likandmydeer/article/details/7939749 一.简介 集合(set)是一组单值对象,它可以是固定的(NSSet).也可以是 ...

  8. NSSet和NSMutableSet - By吴帮雷

    1.NSSet的使用 [NSSet setWithSet:(NSSet *)set]; 用另外一个set对象构造 [NSSet setWithArray:(NSArray *)array];用数组构造 ...

  9. NSArray(二) 、 NSMutableArray 、 NSSet 、 NSMutableSet

    1 创建五个学生对象,放入数组并遍历 1.1 问题 创建一个自定义类TRStudent,为该类生成五个对象.把这五个对象存入一个数组当中,然后遍历数组. 1.2 步骤 实现此案例需要按照如下步骤进行. ...

随机推荐

  1. js世家委托详解

    事件原理 通过div0.addElementListener来调用:用法:div0.addElementListener(事件类型,事件回调函数,是否捕获时执行){}.1.事件类型(type):必须是 ...

  2. RF(For 循环)

    一.介绍:RobotFrameWork 支持 FOR 循环语句,语法和 Python 的语法基本相同,但 RobotFrameWork 中,"FOR" 关键字前面需要增加一个 &q ...

  3. 面试被问为什么使用Spring Boot?答案好像没那么简单

    面试官:项目中有使用Spring Boot吗? 小小白:用过. 面试官:说一下为什么要使用Spring Boot? 小小白:在使用Spring框架进行开发的过程中,需要配置很多Spring框架包的依赖 ...

  4. Java笔记(day23-day26)

     IO流1,复制一个文本文件. 1,明确体系:        源:InputStream ,Reader        目的:OutputStream ,Writer    2,明确数据:       ...

  5. SecureCRT怎么将本级文件上传到CentOS

    进入到想要放文件的路径,不然会默认放在当前路径下: 输入 rz -------------------------------------------------------------------- ...

  6. Java 8 CompletableFuture思考

    Java 8 CompletableFuture思考 最近一直在用响应式编程写Java代码,用的框架大概上有WebFlux(Spring).R2dbc.Akka...一些响应式的框架. 全都是Java ...

  7. node能做的性能优化

    开发中,我们就离不开性能优化,那么在使用node开发的时候,我们可以使用那些代码来优化性能呢 一.释放内存 当node运行检测到错误的时候,释放掉内存 http.get(str,(res)=>{ ...

  8. Try-Catch包裹的代码异常后,竟然导致了产线事务回滚!

    导读:​一段被try-catch包裹后的代码在产线稳定运行了200天后忽然发生了异常,而这个异常竟然导致了产线事务回滚.这期间究竟发生了什么?日常在项目过程中该如何避免事务异常?就在这个时候,老板拿着 ...

  9. 花了几天入门Storm,上了一版,全是Bug

    前言 只有光头才能变强. 文本已收录至我的GitHub精选文章,欢迎Star:https://github.com/ZhongFuCheng3y/3y 听说过大数据的同学应该都听说过Storm吧?其实 ...

  10. 【Spark】SparkStreaming和Kafka的整合

    文章目录 Streaming和Kafka整合 概述 使用0.8版本下Receiver DStream接收数据进行消费 步骤 一.启动Kafka集群 二.创建maven工程,导入jar包 三.创建一个k ...