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



0 0
原创粉丝点击