Objective-c 集合对象
集合(NSSet)是一组单值对象的组合,集合对象的操作包括:搜索,添加,删除集合中的成员(可变集合的功能),比较两个集合,计算两个集合的交集,并集等。
下面来看下(NSSet)的方法:

1)集合的构建
// 构建集合的三种方法
#import <Foundation/Foundation.h>
int main(int argc , const char *argv[]){
@autoreleasepool {
NSSet *set1 = [NSSet setWithObjects:@"zhangsan",@"lisi",@"wangwu",nil];
for(NSString *temp1 in set1){
NSLog(@"temp1 = %@",temp1);
}
NSArray *array = @[@"aa",@"bb",@"cc"];
NSSet *set2 = [NSSet setWithArray:array];
for(NSString *temp2 in set2)
NSLog(@"temp2 = %@",temp2);
NSSet *set3 = [[NSSet alloc] initWithObjects:@"aa",@"bb"@"cc",nil];
for(NSString *temp3 in set3)
NSLog(@"temp3 = %@",temp3);
}
}
2)集合的遍历
#import <Foundation/Foundation.h> @interface NSString (print) - (void)print;
- (void)show:(NSString *)str; @end @implementation NSString (print) - (void)print{
NSLog(@"%@",self);
}
- (void)show:(NSString *)str{
NSLog(@"%@ : %@",str,self);
} @end
int main(int argc , const char *argv[]){
@autoreleasepool {
NSSet *set = [NSSet setWithObjects:@"zhangsan",@"lisi",@"wangwu",nil];
for(NSString *temp in set)
NSLog(@"temp = %@",temp); NSLog(@"--------------------------");
[set enumerateObjectsUsingBlock:^(id obj, BOOL *stop) {
NSLog(@"obj = %@",obj);
}]; NSLog(@"---------------------------");
[set makeObjectsPerformSelector:@selector(print)]; NSLog(@"----------------------------");
[set makeObjectsPerformSelector:@selector(show:) withObject:@"this is "]; NSLog(@"-----------------------------");
NSEnumerator *emr = [set objectEnumerator];
NSString *temp = nil;
while(temp = [emr nextObject])
NSLog(@"temp = %@",temp);
}
return ;
}
3) 集合的比较
#import <Foundation/Foundation.h>
int main(int argc , const char *argv[]){
@autoreleasepool {
NSSet *set = [NSSet setWithObjects:@"zhangsan",@"lisi",@"wangwu",nil];
for(NSString *temp in set)
NSLog(@"temp = %@",temp);
BOOL ishas = [set containsObject:@"lisi"];
if(ishas)
NSLog(@"has lisi");
else
NSLog(@"no lisi");
NSString *str = [set member:@"aaaa"];
NSLog(@"str = %@",str);
NSSet *set2 = [set setByAddingObject:@"xiaoliu"];
NSLog(@"set2 = %@",set2);
NSSet *set3 = [set setByAddingObjectsFromArray:@[@"aa",@"bb",@"cc"]];
NSLog(@"set3 = %@",set3);
NSSet *set4 = [NSSet setWithObjects:@"zhangsan",@"lisi",nil];
BOOL issub = [set4 isSubsetOfSet:set];
if(issub)
NSLog(@"set4 is set sub class");
else
NSLog(@"set4 no set sub class");
BOOL isinterset = [set intersectsSet:set4];
if(isinterset)
NSLog(@"set and set4 has intersect");
else
NSLog(@"set and set4 no intersect");
BOOL isequal = [set isEqualToSet:set2];
if(isequal)
NSLog(@"set = set2");
else
NSLog(@"set != set2");
}
return ;
}
4)可变集合(NSMutable)

下面通过一个例子来说可变集合的用法:
#import <Foundation/Foundation.h> @interface NSString (print)
-(void)print;
-(void)show:(NSString *)str;
@end @implementation NSString(print)
-(void)print{
NSLog(@"%@",self);
}
-(void)show:(NSString *)str{
NSLog(@"%@ : %@",str,self);
}
@end int main(int argc,char **argv){
@autoreleasepool {
NSMutableSet *mset = [NSMutableSet setWithObjects:@"zhangsan",@"lisi",@"wangwu", nil]; [mset addObject:@"zhaoliu"];
NSLog(@"mset = %@",mset); [mset addObjectsFromArray:@[@"",@"",@""]];
NSLog(@"mset = %@",mset); [mset removeObject:@""];
NSLog(@"mset = %@",mset); NSSortDescriptor *sortdesr = [[NSSortDescriptor alloc] initWithKey:nil ascending:YES];
NSArray *sortset = [mset sortedArrayUsingDescriptors:@[sortdesr]];
NSLog(@"mset sort = %@",sortset); NSArray *array = [mset allObjects];
NSLog(@"array = %@",array);
NSSet *set2 = [NSSet setWithArray:array];
NSLog(@"set2 = %@",set2); NSString *str = [mset anyObject];
NSLog(@"str = %@",str); [mset setSet:set2];
NSLog(@"mset = %@",mset); [mset removeAllObjects];
NSLog(@"mset = %@",mset);
}
}
Objective-c 集合对象的更多相关文章
- Asp.net MVC中提交集合对象,实现Model绑定
Asp.net MVC中的Model自动绑定功能,方便了我们对于request中的数据的处理, 从客户端的请求数据,自动地以Action方法参数的形式呈现.有时候我们的Action方法中想要接收数组类 ...
- Set集合对象比较两个元素的方法
Set集合对象比较两个元素的方法并不是根据“equals()”方法的返回值来比较.而是用“hashCode()”方法来进行判断.只要两个元素的“hashCode()”方法的返回值相同,就认为两个元素相 ...
- 转载---Java集合对象的深度复制与普通复制
原博文:http://blog.csdn.net/qq_29329775/article/details/49516247 最近在做算法作业时出现了错误,原因是没有弄清楚java集合的深度复制和浅度复 ...
- Objective-C之集合对象的内存管理
*:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !important; } ...
- Asp.net MVC中提交集合对象,实现Model绑定(转载)
Asp.net MVC中的Model自动绑定功能,方便了我们对于request中的数据的处理, 从客户端的请求数据,自动地以Action方法参数的形式呈现.有时候我们的Action方法中想要接收数组类 ...
- JSon_零基础_004_将Set集合对象转换为JSon格式的对象字符串,返回给界面
将Set集合对象转换为JSon格式的对象字符串,返回给界面 需要导入的jar包: 编写:servlet: package com.west.webcourse.servlet; import java ...
- JSon_零基础_003_将Map集合对象转换为JSon格式的对象字符串,返回给界面
将Map集合对象转换为JSon格式的对象字符串,返回给界面 需导入的jar包: 编写servlet: package com.west.webcourse.servlet; import java.i ...
- java中对集合对象list的几种循环访问
java中对集合对象list的几种循环访问的总结如下 1 经典的for循环 public static void main(String[] args) { List<String> li ...
- 【转】关于spring集合对象的补充
<span style="font-size:18px;">关于spring集合对象的补充 spring2.0中对集合对象有了改进,新增了一个<util>标 ...
随机推荐
- LBYL与EAFP两种防御性编程风格
检查数据可以让程序更健壮,用术语来说就是防御性编程. 检查数据的时候,有这样的两种不同的风格. LBYL:Look Before You Leap EAFP:It's Easier to Ask ...
- 分析java中clone()方法 (转载+修改)
Java中的clone() 方法 java所有的类都是从java.lang.Object类继承而来的,而Object类提供下面的方法对对象进行复制. protected native Object c ...
- ssh login nova vm
$ sudo cat >> /usr/bin/nova-ssh << END FIRST=$1 IDX=`expr index $1 "@"`if [[ ...
- nova的 microversion 实现
之前想写nova的policy的实现, 但是发现网上,有人写的很不错了. 但是个人认为存在一些问题. ref: http://www.cnblogs.com/shaohef/p/4527436.htm ...
- ubuntu中如何关闭防火墙?
只需要输入 root@stgman-desktop:~# sudo ufw disable 防火墙在系统启动时自动禁用
- Redis的持久化选项
Redis提供了两种不同的持久化方法来将数据存储到硬盘里面.一种方法叫快照(snapshotting),它可以将存在于某一时刻的所有数据都写入硬盘里面.另一种方法叫只追加文件(append-only ...
- 2013年 ACM 有为杯 Problem I (DAG)
有为杯 Problem I DAG 有向无环图 A direct acylic graph(DAG),is a directed graph with no directed cycles . T ...
- HTML系列(八):表格
一.基本表格: 表格标记<table>,行标记<tr>,单元格标记<td> 基本语法: <table> <tr> <td>单元格 ...
- ASP.NET MVC开发必看系列
一.关于HTTP协议的那些事 这可以说我们开发WEB程序的空气,推荐不断温故知新! HTTP协议 (一) HTTP协议详解 HTTP协议 (二) 基本认证 HTTP协议 (三) 压缩 HTTP协议 ( ...
- atan(正切函数)
atan函数:返回数值的余切值 原型:double atan(double x) <pre name="code" class="cpp">#inc ...