//
// main.m
// NSSet
//
// Created by New-World on 13-11-2.
// Copyright (c) 2013年 Gary. All rights reserved.
//
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[])
{
@autoreleasepool {
NSSet *set0=[[NSSet alloc] initWithObjects:@"1",@"2",@"3", nil];
NSSet *set1=[NSSet setWithObjects:@"a",@"d",@"f",nil];
NSArray *array=[NSArray arrayWithObjects:@"a",@"d", nil];
NSSet *set2=[NSSet setWithArray:array];
NSSet *set4=[NSSet setWithSet:set1];
NSLog(@"set0:%@",set0);
NSLog(@"set1:%@",set1);
NSLog(@"set2:%@",set2);
NSLog(@"set4:%@",set4);
unsigned long count=[set0 count];//集合中的对象个数
NSArray *allObjects=[set0 allObjects];//以数组的形式返回set0的对象
id object =[set0 anyObject];//获取set0中的任意对象
BOOL isContain=[set1 containsObject:@"f"];//set1中是否包含f
BOOL isInterset=[set0 intersectsSet:set1];//set0与set1是否包含相同元素对象
BOOL isEqual=[set0 isEqualToSet:set1];//set0与set1是否完全相同
BOOL isSubset=[set2 isSubsetOfSet:set1];//set2是否是set1的子集
for (NSString *s in allObjects) {
NSLog(@"s:%@",s);
}
NSLog(@"count: %ld",count);
NSLog(@"object:%@",object);
NSLog(@"set0是否包含:%d",isContain);
NSLog(@"set0,set1是否存在交集:%d",isInterset);
NSLog(@"set0,set1是否完全相同:%d",isEqual);
NSLog(@"set2是否是set1的子集:%d",isSubset);
NSSet *set5=[NSSet setWithObjects:@"one", nil];
NSSet *appset1=[set5 setByAddingObject:@"two"];
NSLog(@"appset:%@",appset1);
NSSet *appset2=[set5 setByAddingObjectsFromSet:set0];
NSLog(@"appset2:%@",appset2);
NSArray *array2=[NSArray arrayWithObject:@"end"];
NSSet *appset3=[set5 setByAddingObjectsFromArray:array2];
NSLog(@"appset3:%@",appset3);
//-------------------NSMutableSet----------------------
NSMutableSet *mset1=[NSMutableSet setWithObjects:@"1",@"2", nil];
NSMutableSet *mset2=[NSMutableSet setWithObjects:@"1",@"3", nil];
//减去相同的内容
[mset2 minusSet:mset1];
//两个set的交集
[mset2 intersectSet:mset1];
//并集
[mset1 unionSet:mset1];
[mset2 removeObject:@"1"];
[mset1 removeAllObjects];
}
return 0;
}
OC之Foundation框架NSSet
最新推荐文章于 2022-05-22 20:21:42 发布