OC NSSet、NSMutableSet、NSArray、NSMutableArray
来源:互联网 发布:c语言非递归遍历算法 编辑:程序博客网 时间:2024/05/22 04:29
不可变长度的Set
@interface NSSet : NSObject <NSCopying, NSMutableCopying, NSSecureCoding, NSFastEnumeration>
初始化方法
+ (instancetype)set; //声明一个空集
+ (instancetype)setWithObject:(id)object;
+ (instancetype)setWithObjects:(const id [])objects count:(NSUInteger)cnt;
+ (instancetype)setWithObjects:(id)firstObj, ... NS_REQUIRES_NIL_TERMINATION;
+ (instancetype)setWithSet:(NSSet *)set;
+ (instancetype)setWithArray:(NSArray *)array;
- (instancetype)init;/* designated initializer */
- (instancetype)initWithObjects:(const id [])objects count:(NSUInteger)cnt;/* designated initializer */
- (instancetype)initWithObjects:(id)firstObj, ... NS_REQUIRES_NIL_TERMINATION;
- (instancetype)initWithSet:(NSSet *)set;
- (instancetype)initWithSet:(NSSet *)set copyItems:(BOOL)flag;
- (instancetype)initWithArray:(NSArray *)array;
可变长度的Set
@interface NSMutableSet : NSSet
对象操作
- (void)addObjectsFromArray:(NSArray *)array;
- (void)intersectSet:(NSSet *)otherSet; //与另个set取交集
- (void)minusSet:(NSSet *)otherSet; //减去一个set
- (void)removeAllObjects;
- (void)unionSet:(NSSet *)otherSet; //与另个set取并集
- (void)setSet:(NSSet *)otherSet; //清空当前set,并将另个set中的元素加进来,简单讲 就是 self=otherSet
初始化
+ (instancetype)setWithCapacity:(NSUInteger)numItems;
- (instancetype)init;/* designated initializer */
- (instancetype)initWithCapacity:(NSUInteger)numItems;
关于初始化还可以用直接赋值的方法:NSSet *set = (@"aa", @77);//不需要在结尾处再弄个nil了
NSLog(@"block 遍历------------");
[set enumerateObjectsUsingBlock:^(id obj, BOOL *stop) {
NSLog(@"%@", obj);
}];
NSLog(@"枚举器 遍历------------");
NSEnumerator* enumerator = [set objectEnumerator];
NSString *str;
while (str = [enumerator nextObject]) {
NSLog(@"%@", str);
}
这两种遍历方法 也适用于 Array
不可变长度Array
@interface NSArray : NSObject <NSCopying, NSMutableCopying, NSSecureCoding, NSFastEnumeration>
常用方法
- (NSUInteger)count;
- (id)objectAtIndex:(NSUInteger)index;
- (id) indexOfObject...
- (id)firstObject;
- (id)lastObject;
- (NSEnumerator *)objectEnumerator;
- (NSEnumerator *)reverseObjectEnumerator; //反向枚举器
- (NSArray *)sortedArrayUsingFunction:(NSInteger (*)(id, id, void *))comparator context:(void *)context; //使用函数排序
- (NSArray *)sortedArrayUsingFunction:(NSInteger (*)(id, id, void *))comparator context:(void *)context hint:(NSData *)hint;
- (NSArray *)sortedArrayUsingSelector:(SEL)comparator; //使用oc方法排序
- (NSArray *)subarrayWithRange:(NSRange)range; //返回范围内的子数组
- (BOOL)writeToFile:(NSString *)path atomically:(BOOL)useAuxiliaryFile; //写入数组到文件
- (BOOL)writeToURL:(NSURL *)url atomically:(BOOL)atomically; //写入数组到url
初始化方法
+ (instancetype)array; //返回一个空数组
+ (instancetype)arrayWithObject:(id)anObject;
+ (instancetype)arrayWithObjects:(const id [])objects count:(NSUInteger)cnt;
+ (instancetype)arrayWithObjects:(id)firstObj, ... NS_REQUIRES_NIL_TERMINATION;
+ (instancetype)arrayWithArray:(NSArray *)array;
- (instancetype)init; /* designated initializer */
- (instancetype)initWithObjects:(const id [])objects count:(NSUInteger)cnt;/* designated initializer */
- (instancetype)initWithObjects:(id)firstObj, ... NS_REQUIRES_NIL_TERMINATION;
- (instancetype)initWithArray:(NSArray *)array;
- (instancetype)initWithArray:(NSArray *)array copyItems:(BOOL)flag;
+ (id /* NSArray * */)arrayWithContentsOfFile:(NSString *)path; //从文件读取出数组
+ (id /* NSArray * */)arrayWithContentsOfURL:(NSURL *)url; //从url
- (id /* NSArray * */)initWithContentsOfFile:(NSString *)path;
- (id /* NSArray * */)initWithContentsOfURL:(NSURL *)url;
可变长度Array
@interface NSMutableArray : NSArray
常用操作方法
- (void)addObject:(id)anObject;
- (void)insertObject:(id)anObject atIndex:(NSUInteger)index;
- (void)removeLastObject;
- (void)removeObjectAtIndex:(NSUInteger)index;
- (void)replaceObjectAtIndex:(NSUInteger)index withObject:(id)anObject;
直接初始化数组: NSArray *array = @[@"aa", @66]; //@66-NSInteger
- OC NSSet、NSMutableSet、NSArray、NSMutableArray
- 集合:NSArray、NSMutableArray、NSDictionary、NSMutableDictionary、NSSet、NSMutableSet使用
- NSSet、NSMutableSet-OC+class
- OC-FUNDATION-NSSet&NSMutableSet
- NSMutableArray,NSArray与NSSet
- iOS开发学习专题-基础知识(二)NSArray数组NSMutableArray可变数组NSSet集合NSMutableSet可变集合详细使用方式
- NSArray、NSMutableArray-OC+class
- OC--NSArray和NSMutableArray
- IOS OC NSArray&NSMutableArray
- OC-NSARRAY-NSMUTABLEARRAY
- 黑马程序员-oc基础-集合NSSet,NSMutableSet
- OC中NSDictionary、NSMutableDictionary、NSSet、NSMutableSet
- OC-045.NSSet类和NSMutableSet类
- oc 知识总结四 (NSSet、NSMutableSet)
- OC--Foundation-NSString,NSArray,NSSet
- OC 中的NSArray NSString NSMutableArray
- OC-NSArray和NSMutableArray
- OC 数组总结(NSArray,NSMutableArray)
- hdu4324
- java 连接 SQL server 数据库时 出现的问题,特别需要注意(包括 版本 区别)
- Android_AFinal框架使用详解
- 有向图的拓扑排序~TopologicalSort
- FPGA开发之数据传输模块
- OC NSSet、NSMutableSet、NSArray、NSMutableArray
- 国外学位论文查找
- 面试题:堆排序
- VSS 在Java开发环境MyEclipse中的应用介绍
- 项目 1 动态链表体验(1)
- UVa 11853 - Paintball <图论+DFS>
- Python装饰器与面向切面编程
- iOSTableView的代理函数
- 视觉直观感受 7 种常用的排序算法