在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

最新文章

  1. [Django]用户权限学习系列之权限管理界面实现
  2. Servlet异步上传文件
  3. java web学习之表单
  4. Ceph剖析:数据分布之CRUSH算法与一致性Hash
  5. easyUI-combotree的本地数据导入
  6. 让Session失效的三种方法
  7. win平台,apache通过web访问svn
  8. disruptor - Concurrent Programming Framework 并发编程框架
  9. wikioi 1154 能量项链
  10. bzoj3677: [Apio2014]连珠线
  11. ubuntu 14.04 修改PS1提示符
  12. P1082丛林探险
  13. 数学函数类方法的使用.java
  14. 使用gSoap做一个简单的CS系统
  15. Eclispse 换主题、皮肤、配色,换黑色主题护眼
  16. ASP.MVC当URL跳转时候参数的安全性
  17. Web API中使用CORS解决跨域
  18. 【LeetCode225】 Implement Stack using Queues★
  19. ECharts修改坐标轴,坐标轴字体,坐标轴网格样式以及控制坐标轴是否显示
  20. [LeetCode] 724. Find Pivot Index_Easy tag: Dynamic Programming

热门文章

  1. Entity framework 加载多层相关实体数据
  2. python selenium(用例断言)
  3. 记一次真实的线上事故:一个update引发的惨案!
  4. 现代企业要求上什么样的MES(四)
  5. 常用linux命令学习记录
  6. Python 为什么抛弃累赘的花括号,使用缩进来划分代码块?
  7. C#学习笔记——数据类型
  8. leetcode-4. 寻找两个正序数组的中位数
  9. [UVA Live 12931 Common Area]扫描线
  10. [hdu5372 Segment Game]树状数组