iOS NSSet和NSMutableSet
2016-04-05 15:12
483 查看
一、简介
集合(set)是一组单值对象,它可以是固定的(NSSet)、也可以是可变的(NSMutableSet)。集合可以比较、计算交集、并集,可变集合还可以有查找、添加、删除。
二、常用方法
#import <Foundation/Foundation.h>
int main (int argc, char *argv[])
{
NSAutoreleasePool*pool=[[NSAutoreleasePool alloc]init];
//集合比较、修改
NSSet *set1=[NSSet setWithObjects:@”s1″,@”s2″,@”s3″,@”s4″,nil];
NSMutableSet *set2=[NSMutableSet setWithObjects:@”s1″,@”s2″,@”s3″,@”s4″,nil];
NSLog(@”List set1″);
for (NSString *element in set1) {
NSLog(@”%@”,element);
}
if ([set1 isEqualToSet:set2]) {
NSLog(@”set1 is equal to set set2″);
} else {
NSLog(@”set1 is not equal to set set2″);
}
if ([set1 containsObject:@”s1″]) {
NSLog(@”set1 contains s1″);
} else {
NSLog(@”set1 not contains s1″);
}
[set2 addObject:@”s5″];
[set2 removeObject:@”s3″];
NSLog(@”List set2″);
for (NSString *element in set2) {
NSLog(@”%@”,element);
}
//集合交集、并集
NSMutableSet *set3;
set3=[NSMutableSet setWithObjects:@”s1″,@”s3″,@”s5″,nil];
[set3 intersectSet:set1];
NSLog(@”135 intersectSet 1234″);
for (NSString *element in set3) {
NSLog(@”%@”,element);
}
set3=[NSMutableSet setWithObjects:@”s1″,@”s3″,@”s5″,nil];
[set3 unionSet:set1];
NSLog(@”135 unionSet 1234″);
for (NSString *element in set3) {
NSLog(@”%@”,element);
}
[pool drain];
return 0;
}
setWithObjects创建包含给定的对象列表的集合
+ (id)setWithObjects:(id)firstObj …
isEqualToSet比较两个集合是否相等
– (BOOL)isEqualToSet:(NSSet *)otherSet
containsObject判断给定的对象是否在集合中
– (BOOL)containsObject:(id)anObject
addObject给集合添加一个对象,如果已有这个对象则不会添加
– (void)addObject:(id)object
removeObject删除集合中给定的对象
– (void)removeObject:(id)anObject
intersectSet取两个集合的交集,如果接收集合中的成员不是给定集合的成员,则从接受集合中删除这个成员。
– (void)intersectSet:(NSSet *)otherSet
unionSet取两个集合的并集,如果给定集合中的成员不是接收集合的成员,则将这个成员添加到接收集合中。
– (void)unionSet:(NSSet *)otherSet
三、NSSet的全部方法
Creating a Set
+ set
+ setWithArray:
+ setWithObject:
+ setWithObjects:
+ setWithObjects:count:
+ setWithSet:
– setByAddingObject:
– setByAddingObjectsFromSet:
– setByAddingObjectsFromArray:
Initializing a Set
– initWithArray:
– initWithObjects:
– initWithObjects:count:
– initWithSet:
– initWithSet:copyItems:
Counting Entries
– count
Accessing Set Members
– allObjects
– anyObject
– containsObject:
– filteredSetUsingPredicate:
– makeObjectsPerformSelector:
– makeObjectsPerformSelector:withObject:
– member:
– objectEnumerator
– enumerateObjectsUsingBlock:
– enumerateObjectsWithOptions:usingBlock:
– objectsPassingTest:
– objectsWithOptions:passingTest:
Comparing Sets
– isSubsetOfSet:
– intersectsSet:
– isEqualToSet:
– valueForKey:
– setValue:forKey:
Creating a Sorted Array
– sortedArrayUsingDescriptors:
Key-Value Observing
– addObserver:forKeyPath:options:context:
– removeObserver:forKeyPath:context:
– removeObserver:forKeyPath:
Describing a Set
– description
– descriptionWithLocale:
四、NSMutableSet的全部方法
Creating a Mutable Set
+ setWithCapacity:
– initWithCapacity:
Adding and Removing Entries
– addObject:
– filterUsingPredicate:
– removeObject:
– removeAllObjects
– addObjectsFromArray:
Combining and Recombining Sets
– unionSet:
– minusSet:
– intersectSet:
– setSet:
本文转载地址:http://blog.it985.com/15878.html
集合(set)是一组单值对象,它可以是固定的(NSSet)、也可以是可变的(NSMutableSet)。集合可以比较、计算交集、并集,可变集合还可以有查找、添加、删除。
二、常用方法
#import <Foundation/Foundation.h>
int main (int argc, char *argv[])
{
NSAutoreleasePool*pool=[[NSAutoreleasePool alloc]init];
//集合比较、修改
NSSet *set1=[NSSet setWithObjects:@”s1″,@”s2″,@”s3″,@”s4″,nil];
NSMutableSet *set2=[NSMutableSet setWithObjects:@”s1″,@”s2″,@”s3″,@”s4″,nil];
NSLog(@”List set1″);
for (NSString *element in set1) {
NSLog(@”%@”,element);
}
if ([set1 isEqualToSet:set2]) {
NSLog(@”set1 is equal to set set2″);
} else {
NSLog(@”set1 is not equal to set set2″);
}
if ([set1 containsObject:@”s1″]) {
NSLog(@”set1 contains s1″);
} else {
NSLog(@”set1 not contains s1″);
}
[set2 addObject:@”s5″];
[set2 removeObject:@”s3″];
NSLog(@”List set2″);
for (NSString *element in set2) {
NSLog(@”%@”,element);
}
//集合交集、并集
NSMutableSet *set3;
set3=[NSMutableSet setWithObjects:@”s1″,@”s3″,@”s5″,nil];
[set3 intersectSet:set1];
NSLog(@”135 intersectSet 1234″);
for (NSString *element in set3) {
NSLog(@”%@”,element);
}
set3=[NSMutableSet setWithObjects:@”s1″,@”s3″,@”s5″,nil];
[set3 unionSet:set1];
NSLog(@”135 unionSet 1234″);
for (NSString *element in set3) {
NSLog(@”%@”,element);
}
[pool drain];
return 0;
}
setWithObjects创建包含给定的对象列表的集合
+ (id)setWithObjects:(id)firstObj …
isEqualToSet比较两个集合是否相等
– (BOOL)isEqualToSet:(NSSet *)otherSet
containsObject判断给定的对象是否在集合中
– (BOOL)containsObject:(id)anObject
addObject给集合添加一个对象,如果已有这个对象则不会添加
– (void)addObject:(id)object
removeObject删除集合中给定的对象
– (void)removeObject:(id)anObject
intersectSet取两个集合的交集,如果接收集合中的成员不是给定集合的成员,则从接受集合中删除这个成员。
– (void)intersectSet:(NSSet *)otherSet
unionSet取两个集合的并集,如果给定集合中的成员不是接收集合的成员,则将这个成员添加到接收集合中。
– (void)unionSet:(NSSet *)otherSet
三、NSSet的全部方法
Creating a Set
+ set
+ setWithArray:
+ setWithObject:
+ setWithObjects:
+ setWithObjects:count:
+ setWithSet:
– setByAddingObject:
– setByAddingObjectsFromSet:
– setByAddingObjectsFromArray:
Initializing a Set
– initWithArray:
– initWithObjects:
– initWithObjects:count:
– initWithSet:
– initWithSet:copyItems:
Counting Entries
– count
Accessing Set Members
– allObjects
– anyObject
– containsObject:
– filteredSetUsingPredicate:
– makeObjectsPerformSelector:
– makeObjectsPerformSelector:withObject:
– member:
– objectEnumerator
– enumerateObjectsUsingBlock:
– enumerateObjectsWithOptions:usingBlock:
– objectsPassingTest:
– objectsWithOptions:passingTest:
Comparing Sets
– isSubsetOfSet:
– intersectsSet:
– isEqualToSet:
– valueForKey:
– setValue:forKey:
Creating a Sorted Array
– sortedArrayUsingDescriptors:
Key-Value Observing
– addObserver:forKeyPath:options:context:
– removeObserver:forKeyPath:context:
– removeObserver:forKeyPath:
Describing a Set
– description
– descriptionWithLocale:
四、NSMutableSet的全部方法
Creating a Mutable Set
+ setWithCapacity:
– initWithCapacity:
Adding and Removing Entries
– addObject:
– filterUsingPredicate:
– removeObject:
– removeAllObjects
– addObjectsFromArray:
Combining and Recombining Sets
– unionSet:
– minusSet:
– intersectSet:
– setSet:
本文转载地址:http://blog.it985.com/15878.html
相关文章推荐
- iOS - 正则表达式判断邮箱、手机号码验证、车牌号验证、车型、用户名、密码、昵称、身份证号
- iOS开发的一些奇巧淫技
- Thinkpad E450c进入BIOS
- iOS 支付宝集成获取私钥与公钥
- iOS 页面间几种传值方式(属性,代理,block,单例,通知)
- ios 坐标系的转换
- 【iOS】系统框架学习
- iOS开发之ReplayKit框架学习
- ios学习--正则表达式示例
- iOS一个简单聊天工具的实现
- iOS键盘数字判断
- ios学习--正则表达式匹配规则
- ios学习--正则表达式运算符优先级
- ios学习--正则表达式元字符
- 截取PDF的某一页 iOS
- ios学习--正则表达式非显式的元字符
- iOS项目中为控件定义输出口连线
- iOS OC与JS,JS与OC交互
- ios进制转换
- ios后台长时间运行的选择策略及注意事项