iOS 中NSArray NSSet转换 过滤重复与排序操作

IOS防止数组越界 移动开发中会经常遇到数组越界问题,导致程序闪退,导致应用界面不友好。我们可以从根本上解决数组越界导致的程序闪退 立即下载

很久没写ios代码。最近和朋友聊天时候有强迫症,想强烈把代码给优化。

问题是这样的:现在数据是日期数组,想过滤掉重复的日期,怎么解决。

原始代码


这里想到更简洁点。用NSSet来搞定。

NSArray *arr = @[@"12-11", @"12-11", @"12-11", @"12-12", @"12-13", @"12-14"];
NSSet *set = [NSSet setWithArray:arr];
NSLog(@"set%@\n", set);
打印出来的结果

{(
    "12-11",
    "12-14",
    "12-13",
    "12-12"
)}

朋友看了说不行,这样打印的结果是无序的,用户要看有序的结果。

查了一下NSSet的排序,发现有个

@interface NSSet (NSSortDescriptorSorting)

- (NSArray *)sortedArrayUsingDescriptors:(NSArray *)sortDescriptors NS_AVAILABLE(10_6, 4_0);    // returns a new array by sorting the objects of the receiver

@end

传递的参数是NSSortDescriptor数组。

因此修改后代码如下

NSArray *arr = @[@"12-11", @"12-11", @"12-11", @"12-12", @"12-13", @"12-14"];
NSSet *set = [NSSet setWithArray:arr];
NSArray *sortDesc = @[[[NSSortDescriptor alloc] initWithKey:nil ascending:YES]];
NSArray *sortSetArray = [set sortedArrayUsingDescriptors:sortDesc];
NSLog(@"set%@\nsort set%@", set, sortSetArray);

打印出来结果

{(
    "12-11",
    "12-14",
    "12-13",
    "12-12"
)}
 sort set(
    "12-11",
    "12-12",
    "12-13",
    "12-14"
)

发现NSSortDescriptor是专门用来排序的描述符。可以用来排序NSSet,NSDictionary,NSArray。测试一下数组的使用。代码如下

NSArray *arr = @[@"12-11", @"12-11", @"12-11", @"12-12", @"12-13", @"12-14"];
NSArray *sortDesc = @[[[NSSortDescriptor alloc] initWithKey:nil ascending:YES]];
NSArray *sort1Array = [arr sortedArrayUsingDescriptors:sortDesc];
NSLog(@"sort1 array: %@\n",sort1Array);

打印出来

sort1 array: (
    "12-11",
    "12-11",
    "12-11",
    "12-12",
    "12-13",
    "12-14"
)

想起来一直用NSArray的排序代码

NSArray *arr = @[@"12-11", @"12-11", @"12-11", @"12-12", @"12-13", @"12-14"];
NSArray *sort2Array = [arr sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
        return [obj1 localizedCompare:obj2];
    }];
NSLog(@"sort2 array %@", sort2Array);


以上只是示例,具体NSSortDescriptor和array的高级用法,大家可以查文档,可以简化代码。

【v4l2】在Android SDK中交叉编译v4l2-ctl 为调试Android设备中camera的相关功能,需要引入v4l2-ctl工具。由于厂商提供的Android SDK源码中未集成v4l2-utils(包含v4l2-ctl以及其它组件的工具包),故需要下载v4l2-utils源码,并在Android SDK中交叉编译生成v4l2-ctl可执行文件。本文讲述其过程步骤。 阅读详情

相关推荐

从FER2013到AffectNet:表情识别数据集十年演进史与你的项目选型攻略

本文回顾了从FER2013到AffectNet等主流人脸表情识别数据集的十年演进历程,详细分析了各数据集的特点、适用场景及技术突破。针对不同项目需求,提供了从移动端应用到高精度研究的选型策略,特别强调AffectNet在现代FER项目中的核心价值,并给出混合数据集构建的实战方案。

weixin_26808439的博客 521

NSSet和NSArray转化

//如果想set排序以后再取,可以这样: NSSet *users = [groupUser users]; NSSortDescriptor *sd = [[NSSortDescriptor alloc] initWithKey:@"displayName" ascending:YES]; NSArray *sortDescriptors = [NSArray arrayWithObjec

FengziTianYu的专栏 1861

Codex Skill:AI编程助手的专项能力扩展与实战指南

在AI编程助手领域,Skill(技能)是一种标准化的能力扩展机制,它通过封装特定任务所需的指令、资源和脚本,解决AI模型在专项任务上的能力局限。其核心原理是将专业知识和工作流程模块化,让通用AI助手能够像领域专家一样执行复杂任务。从技术价值看,Skill不仅提升了代码生成的准确性,更重要的是实现了业务流程的自动化集成,如代码规范检查、API文档生成等具体场景。这种机制特别适用于需要结合内部工具链和特定业务逻辑的企业环境,能够显著提升开发效率和质量。通过安装PPT生成、SpringBoot项目创建等专项Ski

weixin_33672109的博客 413

QGroundControl或者华科尔地面站安装完打不开问题解决

QGroundControl 华科尔 打不开

wv112406的博客 4891

OpenBullet2 学习研究2-Conditions条件之KeyCheck的使用

上一节我们学习了如何安装Openbullet2以及如何新建一个配置,最后我们再配置里面新建一个Http的请求块,帮助我们发送了第一个请请求。这一节我们来学习check块。当我们发送请求以后,请求返回的内容是否是我们想要的,或者请求发送正确没有,这个我们怎么判断呢?这个时候就要用到check块了。

956

Set和Array之间相互转换

// Array 转 Set var mySet = new Set(["value1", "value2", "value3"]); // 用...操作符,将 Set 转 Array var myArray = [...mySet]; 数组去重 var mySet = new Set([1, 2, 3, 4, 4]); [...mySet]; // [1, 2, 3, 4]

dongwang的博客 2095

Python对接海康威视机器视觉工业相机

工业相机只允许单条连接,也就是说MVS如果连接了相机,python代码就无法获取数据,此时必须。相机工具类(读者直接复制)HKCamera_class.py。博主这里出现了一个警告:获取不到相机参数。四、使用Pycharm打开Python代码示例文件夹。三、找到MVS示例代码(代码在MVS的安装位置)二、解压并安装MVS客户端。一、下载MVS客户端。安装一下opencv。

Good Luck 2万+

NSArray,NSSet,NSDictionary

原文地址:NSArray,NSSet,NSDictionary作者:醉吻花颜 NSArray与NSSet NSArray:有序的集合,存储的元素在一个整块的内存中并按序排列; NSSet:无序的集合,散列存储。 读developer.apple关于NSSet的解释:You can use sets as an alternative to arrays when the order

836

基于Matlab的自动泊车系统开发:利用模糊控制实现智能停车解决方案的详细教程

Matlab是一种广泛应用于科学计算、工程建模与仿真的编程语言,其强大的工具箱和简洁的编程语法,使其成为开发自动泊车系统的理想选择。在开始编写代码之前,我们需要搭建Matlab的工作环境,并熟悉相关的工具箱,如Fuzzy Logic Toolbox,用于实现模糊控制。首先,确保你已经安装了Matlab软件,并加载了必要的工具箱。接下来,我们将逐步讲解如何在Matlab中实现自动泊车系统。

qq_38334677的博客 662

Unity 工具 之 SoftMask软遮罩 实现 UI 边缘渐变过渡的简单使用介绍

目录Unity 工具 之 SoftMask软遮罩 实现 UI 边缘渐变过渡的简单使用介绍一、简单介绍二、Mask 实现的遮罩效果三、Soft Mask 实现遮罩效果四、 Soft Mask 的一些设置五、插件下载Unity 工具类,自己整理的一些游戏开发可能用到的模块,单独独立使用,方便游戏开发。

仙魁XAN 8574

NSSet NSArray的区别 实际用法 IOS

转载请说明:(谢谢) http://blog.youkuaiyun.com/a21064346/article/details/8055797 点击打开链接 最近在做一个项目,对数据处理这块有一点心得。 如何在添加大量数据时,对数据对象进行筛选只做到数据对象添加一次? 那么你脑子中,就应该有一个 集合,数组的概念了。(NSSet,NSArray我是这么来理解的)。

a283127993的专栏 3159

ue 蓝图 c++ websocket 事件回调

ue 蓝图 c++ websocket 事件回调

jacke121的专栏 166

NSSet和NSArray的区别

转载请说明:(谢谢) http://blog.youkuaiyun.com/a21064346/article/details/8055797 点击打开链接 最近在做一个项目,对数据处理这块有一点心得。 如何在添加大量数据时,对数据对象进行筛选只做到数据对象添加一次? 那么你脑子中,就应该有一个 集合,数组的概念了。(NSSet,NSArray我是这么来理解的)。

dr19901106的博客 605

jre9下载 jdk9

(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

iOS学习-- Foundation框架

热乎乎的周报来咯

2301_80253909的博客 995

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

【iOS】—— Foundation框架(四)- 有序集合(NSOrderedSet与NSMutableOrderedSet)

有序集合(NSOrderedSet与NSMutableOrderedSet) NSOrderedSet与NSMutableOrderedSet既有NSSet集合的特征,也具有NSArray类似的功能。 NSOrderedSet不允许元素重复,这与NSSet集合相同。 NSOrderedSet可以保持元素的添加顺序,而且每个元素都有索引,可以根据索引来操作元素。这与NSArray的功能类似。 NSMutableOrderedSet是NSOrderedSet的子类,代表集合元素可变的有序集合。NSMuta

m0_55124878的博客 986

Data-Structures-and-Algorithm-Analysis-in-Java.pdf

作者: Weiss, Mark A. 出版社: Addison Wesley Data Structures and Algorithm Analysis in Java is an “advanced algorithms” book that fits between traditional CS2 and Algorithms Analysis courses. In the old ACM Curriculum Guidelines, this course was known as CS7. This text is for readers who want to learn good programming and algorithm analysis skills simultaneously so that they can develop such programs with the maximum amo...

OC学习之——Foundation框架

OC集合类可以用于存储数量不等的多个对象,并且可以实现常用的数据结构,例如栈和队列等,除此之外,OC集合还可以用来保存具有映射关系的关联数组。NSArray代表有序、可重复的集合,很像一个数组NSSet代表无序、不可重复的集合代表具有映射关系的集合在实际编程里,面向的是NSArray(及其子类NSMutableArray)、NSSet(及其子类NSMutableSet)、NSDictionary(及其子类NSMutableDictionary)编程,程序创建的也可能是它们的子类的实例。

m0_73348697的博客 1089

Foundation框架详解

重写isEqual:和hash方法(isEqual:方法判断相等,hash返回值相等)实现copyWithZone:方法,最好返回该对象的不可变副本为了避免索引(key)被破坏,NSDictionary总会先调用该key的copy方法复制该对象的不可变副本,然后用该副本作为NSDictionary的key。

2401_87841908的博客 1008

如何将Set转换为Array?

Set seems like a nice way to create Arrays with guaranteed unique elements, but it does not expose

w36680130的博客 2909

iOS中集合的互相转换

//1.NSArray 转换成 NSMutableArray         NSArray * array = @[@"one",@"two",@"three"];         NSMutableArray * muArray = [NSMutableArray arrayWithArray:array];         NSLog(@"muarray %@",muArray);

王俊岭的博客 1401

【IOS 开发】Objective-C Foundation 框架 -- 字符串 | 日期 | 对象复制 | NSArray | NSSet | NSDictionary | 谓词

Objective - C Foundation 框架

让 学习 成为一种 习惯 ( 韩曙亮 の 技术博客 ) 3523

iOS_NSSet与NSArray的区别

NSSet和NSArray的区别。

司小文的专栏 5180

iOS开发学习专题-基础知识(二)NSArray数组NSMutableArray可变数组NSSet集合NSMutableSet可变集合详细使用方式

本文主要讲解的是不可变数组NSArray可变数组NSMutableArray集合NSSet可变集合NSMutableSet详细使用方式包括类型和值之间的转换 文章是博主原创,转载请标明出处 首先来一个集合的详细方法~ #pragma mark NSSet 不可变集合 // NSSet和NSArray有相似之处,都是存储不同的对象的地址 // 但是NSArray是有序的集合,N

伪随机的张三 4649

NSSet和 NSArray

1、求数组的 交集,并集,差集 1 2 3 4 5 6 7 8 NSArray *array1 = @[@"1",@"2",@"3"]; NSArray *array2 = @[@"1",@"5",@"6"]; NSMutableSet *set1 = [NSMutableSet setWithArray:array1]

Coder 482
上一篇: mac os x 10.9 U盘通过 Boot Camp Assistant 安装windows7
下一篇: 吐嘈一下xmind1个设计
watsy
博客等级 码龄19年 133粉丝 104原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值