在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. Arduino入门简介

    先说Arduino是什么? 1.一个平台,开源电子原型平台,包含小板子(UNO开发板.PRO Mini板登)和电脑上安装的软件(IDE). 2.能通过传感器(红外.温度.超声波传感器...)等来感知环 ...

  2. MySQL JDBC Driver 8.0+设置服务器时区

    遇到一个问题,线下环境测试数据的查询完全没有问题,但是线上环境却没法查询出数据,并且从mybatis输出的日志来看,查询参数也没有问题,数据库中数据也是存在的,查询参数类型是java.util.Dat ...

  3. Cordova 浅析架构的原理

    因为项目使用了Cordova,也使用了很长时间.至于有很多hybride框架,为什么我们使用Cordova,这里不做过多的叙述,我们也是根据项目需求来选定的,需要及时更新.还要输出别人SDK等.没有最 ...

  4. JAVA设计模式之单例(singleton)

    一.饿汉式 /** * 饿汉式 */public class Singleton01 { private static final Singleton01 instance = new Singlet ...

  5. Offset等一些类似属性的使用

    1.offset系列 // offset 系列 var father = document.querySelector('.father'); var son = document.querySele ...

  6. vue-multi-module【多模块集成的vue项目,多项目共用一份配置,可以互相依赖,也可以独立打包部署】

    基于 vue-cli 2 实现,vue 多模块.vue多项目集成工程 Github项目地址 : https://github.com/BothEyes1993/vue-multi-module 目标: ...

  7. C#实现局域网聊天 通讯 Socket TCP 多人

    程序分别为服务端与客户端,服务端创建套接字使用多线程侦听多客户端请求 代码需要引用System.Net:和System.Net.Socket:这两个类 分享源码demo:https://pan.bai ...

  8. 我的linux学习日记day7

    一.文件权限: r:read 读取文件列表的权限, 数字4表示 w:write 写入.删除.修改的权限,数字2表示 x:execute 进入目录的权限,数字1表示 权限分配:文件所有人.文件所属主.其 ...

  9. node的fs模块

    node的file system模块提供的api有同步和异步两种模式(大多数情况下都是用的异步方法,毕竟异步是node的特色,至于提供同步方法,可能应用程序复杂的时候有些场景使用同步会比较合适).异步 ...

  10. css3弹性布局

    二.弹性布局(重点******************************************) 1.什么是弹性布局 弹性布局,是一种布局方式. 主要解决的是某个元素中子元素的布局方式 让页面 ...