码迷,mamicode.com
首页 > 其他好文 > 详细

cocoa foundation笔记-4

时间:2014-08-02 18:34:34      阅读:186      评论:0      收藏:0      [点我收藏+]

标签:os   io   ar   log   app   table   har   c   

//NSSet类是一组单值对象的集合,且NSSet实例中元素是无序的,同一个对象只能保存一个

/*******************不可变集合****************/
//集合的创建
//方式一
NSSet *set1 = [NSSet setWithObject:@"1", @"2", nil];
//方式二
NSSet *set2 = [[NSSet alloc] initWithObejects:@"1", @"2", @"3", nil];
//方式三
NSArray *array1 = [NSArray arrayWithObjects:@"4", @"5", nil];
NSSet *set3 = [NSSet setWithArray:array1];
//方式四
NSSet *set4 = [NSSet setWithSet:set1];

//获取集合元素个数
int count = [set2 count];

//获取集合元素
NSArray *objects = [set2 allObjects];
NSLog(@"%@", objects);    //输出:(1,2,3)

//获取集合中任意一个对象
id object = [set2 anyObject];

//判断集合是否包含某元素
BOOL isContain = [set4 containsObject:@"1"];    //输出:isContain=1

//判断集合间是否存在交集
BOOL isIntersect = [set1 intersectSet:set2];

//判断是否匹配(元素都相同)
BOOL isEqual = [set1 isEqualToSet:set2];

//判断一个集合是否是另一个集合的子集
BOOL isSub = [set1 isSubsetOfSet:set2];

//追加一个元素
NSSet *set5 = [NSSet setWithObjects:@"one"];
NSSet *appSet1 = [set5 setByAddingObject:@"two"];    //输出:appSet1={one,two}

//追加一个集合
//方式一
NSSet *appSet2 = [set5 setByAddingObjectsFromSet:set3];
//方式二
NSArray *array2 = [NSArray arrayWithObject:@"end"];
NSSet *appSet3 = [set5 setByAddingObjectsFromArray:array2];

/***************可变集合********************/
//创建一个空的集合
NSMutableSet *set1 = [NSMutableSet set];
NSMutableSet *set2 = [NSMutable setWithObjects:@"1", @"2", nil];
NSMutableSet *set3 = [NSMutable setWithObjects:@"a", @"2", nil];

//集合2“减去”集合3中的元素,集合2最后元素只有一个,且为1
[set2 minusSet:set3];

//集合2与集合3中元素的交集,集合2最后元素只有一个,且为2
[set2 intersectSet:set3]

//集合2与集合3中元素的并集,集合2最后元素有三个,为1,2,a
[set2 unionSet:set3];

//将空集合1设置为集合3中的内容
[set1 setSet:set3];

//根据数组中的内容删除集合中的对象
[set2 removeObjectsFromArray:array];
[set2 removeObject:@"1"];
[set2 removeAllObjects];


cocoa foundation笔记-4,布布扣,bubuko.com

cocoa foundation笔记-4

标签:os   io   ar   log   app   table   har   c   

原文地址:http://my.oschina.net/Jacedy/blog/297391

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!