#私藏项目实操分享#iOS开发:NSSet的使用

前言

在iOS开发过程中,有些时候会用到去重处理,也就是去掉重复的元素或者个数,然后再进行排序处理,这就使用到了集合(NSSet),它其实是一种哈希表,通过运用散列算法来查找集合中的元素。

集合和数组的相同点:都是存储不同元素的地址,不同点:NSSet中的元素都是被自动过滤之后的不会重复的元素,NSArray中的元素却是允许重复的;NSSet是一个无顺序的集合,NSArray是一个有顺序的集合。相对来说,NSSet的处理效率比NSArray的要快。

本节内容就来简单的介绍一下NSSet的使用,并且会举一个实际使用案例来展示。

一、基本常用的方法

(一)NSSet
1、集合初始化

NSSet在初始化的时候,赋值的时候如果放入两个相同元素的话,会自动删除其中一个相同的元素。eg:
NSSet *set1 = [[NSSet alloc] initWithObjects:@“a”,@“b”,@“c”,@“d”, nil]; //初始化集合
[set1 count]; //返回集合中的个数
2、NSSet的元素处理

NSMutableSet *mSet1 = [[NSMutableSet alloc] initWithObjects:@"1",@"2",@"3", nil];
NSMutableSet *mSet2 = [[NSMutableSet alloc] initWithObjects:@“1”,@“4”,@“5”, nil];
[mSet1 unionSet:mSet2]; //取出并集1,2,3,4,5
[mSet1 intersectSet:mSet2]; //取交集1
[mSet1 minusSet:mSet2]; //删除mSet1中与mSet2相同的元素 结果为:2,3,4,5
3、判断集合中是否含有某个元素

BOOL ret1 = [set1 containsObject:@“Apple”];  //判断集合中是否含有@“Apple”
4、判断两个集合是否相等
NSSet *set2 = [[NSSet alloc] initWithObjects:@“a”,@“b”,@“c”,@“d”, nil];
BOOL ret1 = [set isEqualToSet:set2]; //判断两个集合是否相等
5、判断set2是否是set3的子集

NSSet *set3 = [[NSSet alloc] initWithObjects:@“a”,@“b”,@“c”,@“d”,@“e”, nil];
BOOL ret2 = [set2 isSubsetOfSet:set3]; //判断set2是否是set3的子集合
6、集合用枚举器来遍历

NSEnumerator  *enumerator = [set objectEnumerator];
NSString *str;
while (str = [enumerator nextObject]) {
……
}
7、根据数组来初始化集合,也就是数组转换为集合

NSArray  *array1 = [[NSArray alloc] initWithObjects:@“1”,@“2”,@“3”,@“4”, nil];
NSSet *set = [[NSSet alloc] initWithArray:array1];
8、集合转换为数组

NSArray  *array2 = [set allObjects];
(二)NSMutableSet
1、可变集合初始化

NSMutableSet *mSet1 = [NSMutableSet setWithObjects:@"1", @“2”, @“3”, @“4”, nil];  //可变集合初始化
NSMutableSet *mSet2 = [NSMutableSet setWithCapacity:0]; //可变集合
2、添加元素

[mSet1 addObject:@“5”];
[mSet1 addObject:@“6”];
[mSet1 addObject:@“6”]; //如果添加的元素有重复,实际情况只保留一个相同的元素
3、删除元素

[mSet1 removeObject:@“3”];
[mSet1 removeAllObjects];
4、NSIndexSet:指数集合(索引集合)

NSIndexSetNSIndexSet  *indexSet1 = [[NSIndexSet alloc] initWithIndexesInRange:NSMakeRange(1,3)]; //集合中的数字是1 2 3
//根据集合提取数组中指定位置的元素
NSArray *array1 = [[NSArray alloc] initWithObjects:@“1”,@“2”,@“3”,@“4”, nil];
NSArray *array2 = [array1 objectsAtIndexes:indexSet]; //返回2 3 4
5、NSMutableIndexSet:可变指数集合

NSMutableIndexSet *indexSet2 =[[NSMutableIndexSet alloc] init];
[indexSet2 addIndex:0];
[indexSet2 addIndex:2];
[indexSet2 addIndex:4]; //通过集合获取数组中指定元素
NSArray *array3 = [[NSArray alloc] initWithObjects:@“1”,@“2”,@“3”,@“4”,@“5”,@“6”, nil];
NSArray *array4 = [array3 objectsAtIndexes:indexSet]; //返回1 3 5

二、案例展示

这里的案例是为了在做本地的存储数据之前,先把重复的数据进行一个去重操作,然后再把这些数据进行本地存储。封装了一个工具类来进行系统化处理,具体代码及使用地方如下所示:

1、工具类.h文件:

#import <Foundation/Foundation.h>
@interface JSHistoryTool : NSObject
• (instancetype)sharedHistoryTool;

// 存储 **操作历史记录
• (void)writeTemp:(NSString *)temp time:(NSString *)time;

// 取出 **操作历史记录
• (NSArray *)getValue;

@end
#私藏项目实操分享#iOS开发:NSSet的使用
2、工具类.m文件:

#import "JSHistoryTool.h"
@implementation JSHistoryTool
• (instancetype)sharedHistoryTool {

static id instance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
instance = [[self alloc]init];
});
return instance;
}
• (void)writeTemp:(NSString *)temp time:(NSString *)time {

NSArray *arr = [[NSUserDefaults standardUserDefaults] valueForKey:@"set"];
NSMutableSet *sSet = [[NSMutableSet alloc] initWithArray:arr]; //先把现有存储的数组取出来,然后根据数组元素进行去重,最后再进行存储
[sSet addObject:@{@"Temp":temp, @"Time":time}];
NSArray *aa = [sSet allObjects];
NSMutableArray *marr = [[NSMutableArray alloc] initWithArray:aa];
if (marr.count > 20) {
[marr removeObjectAtIndex:0];
}
[[NSUserDefaults standardUserDefaults] setObject:marr forKey:@"set"];
}
• (NSArray *)getValue {

NSArray *arr = [[NSUserDefaults standardUserDefaults] valueForKey:@"set"];
return [[arr reverseObjectEnumerator] allObjects];
}
@end
#私藏项目实操分享#iOS开发:NSSet的使用
3、控制器中具体调用地方的代码:

(void)sendClick {
if (![JSBLECenterManager sharedInstance].characteristic) {
return;
}
_tempValue = [NSString stringWithFormat:@"%.0f", _centigradeDegree];
_timeValue = [NSString stringWithFormat:@"%.0ld", (long)_timeDegree];
_waterValue = [NSString stringWithFormat:@"%.0f", _waterDegree];
NSString *ttData = [NSString stringWithFormat:@"TEA%@,%@,%@U", _tempValue, _timeValue, _waterValue];
NSMutableSet *set1 = [[NSMutableSet alloc] initWithObjects:_tempValue, _timeValue, nil]; //初始化可变集合
[[JSHistoryTool sharedHistoryTool] writeTemp:_tempValue time:_timeValue]; //通过工具类进行存储操作
NSData *data = [ttData dataUsingEncoding:NSUTF8StringEncoding];
// 根据上面的特征self.characteristic来写入数据
[[JSBLECenterManager sharedInstance].peripheral writeValue:data forCharacteristic:[JSBLECenterManager sharedInstance].characteristic type:CBCharacteristicWriteWithResponse];
}

#私藏项目实操分享#iOS开发:NSSet的使用

最后

以上就是本章的全部内容,欢迎关注三掌柜的微信公众号“程序猿by三掌柜”,三掌柜的新浪微博“三掌柜666”,欢迎关注!

文章版权声明

 1 原创文章作者:天道酬勤,如若转载,请注明出处: https://www.52hwl.com/35601.html

 2 温馨提示:软件侵权请联系469472785#qq.com(三天内删除相关链接)资源失效请留言反馈

 3 下载提示:如遇蓝奏云无法访问,请修改lanzous(把s修改成x)

 免责声明:本站为个人博客,所有软件信息均来自网络 修改版软件,加群广告提示为修改者自留,非本站信息,注意鉴别

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023年7月15日 下午12:25
下一篇 2023年7月15日 下午12:26