NSMutableSet是NSSet的子类,在NSSet的基础上声明了管理可变无序集合的接口。
关于NSSet的编程介绍详见《NSSet Class Reference》
初始化
+ setWithCapacity:
- initWithCapacity:
#pragma mark 初始化
- (void)testCreating {
// 创建可变集合,并设置初始的内部元素个数
NSMutableSet *mSet = [NSMutableSet setWithCapacity:10];
mSet = [[NSMutableSet alloc] initWithCapacity:10];
}
增加和删除
- addObject:
- filterUsingPredicate:
- removeObject:
- removeAllObjects
- addObjectsFromArray:
#pragma mark 增加和删除
- (void)testAddingAndRemoving {
NSMutableSet *mSet = [NSMutableSet set];
// 增加
[mSet addObject:@"阳君"];
// 同时增加多个对象
NSArray *array = [NSArray arrayWithObjects:@"阳君", @"937447974", @"937447974", nil];
[mSet addObjectsFromArray:array];
// 过滤
NSPredicate *predicate = [NSPredicate predicateWithValue:YES];
[mSet filterUsingPredicate:predicate];
// 删除单一对象
[mSet removeObject:@"阳君"];
// 删除全部
[mSet removeAllObjects];
}
结合和重组
- unionSet:
- minusSet:
- intersectSet:
- setSet:
#pragma mark 结合和重组
- (void)testCombiningAndRecombining {
NSMutableSet *mSet = [NSMutableSet setWithObjects:@"阳君", @"937447974", nil];
NSMutableSet *mOtherSet = [NSMutableSet setWithObjects:@"阳君", @"IOS", nil];
// 并集
[mSet unionSet:mOtherSet];
// 差集
[mSet minusSet:mOtherSet];
// 交集
[mSet intersectSet:mOtherSet];
// 全替换
[mSet setSet:mOtherSet];
}
其他
参考资料
NSMutableSet Class Reference
Collections Programming Topics
文档修改记录
时间 | 描述 |
---|---|
2015-10-21 | 根据IOS9的NSMutableSet的API总结 |