NSArray 冷知识

NSArray

用属性表示一个数组的内容。

- (NSString *)descriptionWithLocale:(nullable id)locale;

- (NSString *)descriptionWithLocale:(nullable id)locale indent:(NSUInteger)level;

- (NSString *)description

正序倒序

- (NSEnumerator<ObjectType> *)objectEnumerator;

- (NSEnumerator<ObjectType> *)reverseObjectEnumerator;

    //1.原始数组
    NSMutableArray *array = [NSMutableArray arrayWithObjects:@"1",@"2",@"3",nil];
    //2.倒序的数组
    NSArray* reversedArray = [[array reverseObjectEnumerator] allObjects];


    //2、枚举器法
    NSLog(@"------- 枚举器法---------");
    //ObjectEnumerator        正序
    //reverseObjectEnumerator 逆序
    NSEnumerator *enumerator = [array reverseObjectEnumerator];
    //不确定数组里面具体对象的类型,所以定义成id 类型指针
    id obj = nil;  
    while (obj = [enumerator nextObject]) { 
         //通过枚举器,取数组里面的每一个元素
        NSLog(@"%@", obj);                 
        //将元素赋给 obj, 直到数组结束
        //取到的结果为nil,退出while
    }

用来数组排序的参数 、排序顺序、

@property (readonly, copy) NSData *sortedArrayHint;

- (NSArray<ObjectType> *)sortedArrayUsingFunction:(NSInteger (NS_NOESCAPE *)(ObjectType, ObjectType, void * _Nullable))comparator context:(nullable void *)context;

- (NSArray<ObjectType> *)sortedArrayUsingFunction:(NSInteger (NS_NOESCAPE *)(ObjectType, ObjectType, void * _Nullable))comparator context:(nullable void *)context hint:(nullable NSData *)hint;

使用NSArray自己的sel方法来排序,如果自定义要创建NSArray的category

- (NSArray<ObjectType> *)sortedArrayUsingSelector:(SEL)comparator;

是否已经把文件保存到辅助文件 或者 网上的URL中

- (BOOL)writeToFile:(NSString *)path atomically:(BOOL)useAuxiliaryFile;

- (BOOL)writeToURL:(NSURL *)url atomically:(BOOL)atomically;

将一个操作方法作用在数组中的每个元素上.。(不带参数/带参数)

- (void)makeObjectsPerformSelector:(SEL)aSelector

- (void)makeObjectsPerformSelector:(SEL)aSelector withObject:(id)argument;

返回指定下标的一个对象。

- (ObjectType)objectAtIndexedSubscript:(NSUInteger)idx;

数组的遍历

- (void)enumerateObjectsUsingBlock:(void (NS_NOESCAPE ^)(ObjectType obj, NSUInteger idx, BOOL *stop))block ;

数组的遍历,新加遍历顺序

- (void)enumerateObjectsWithOptions:(NSEnumerationOptions)opts usingBlock:(void (^)(id obj, NSUInteger idx,BOOL *stop))block

数组某一部分的遍历,新加遍历顺序

- (void)enumerateObjectsAtIndexes:(NSIndexSet *)s options:(NSEnumerationOptions)opts usingBlock:(void (NS_NOESCAPE ^)(ObjectType obj, NSUInteger idx, BOOL *stop))block;

根据条件用来获取一个NSUIndex 对象,主要是根据条件进行数据遍历使用

- (NSUInteger)indexOfObjectPassingTest:(BOOL (NS_NOESCAPE ^)(ObjectType obj, NSUInteger idx, BOOL *stop))predicate ;

NSInteger index = [array indexOfObjectPassingTest:^ BOOL (id tr,NSUInteger index, BOOL *te){
        NSString *s = (NSString *)tr;
        if([@"wendy" isEqualToString:s])
        {
            return YES;
        }
        return NO;
}];

NSLog(@"index==%d=.",index);

选择数组的遍历顺序,来获取一个NSUindex对象

- (NSUInteger)indexOfObjectWithOptions:(NSEnumerationOptions)opts passingTest:(BOOL (NS_NOESCAPE ^)(ObjectType obj, NSUInteger idx, BOOL *stop))predicate ;

子数组 遍历 处理数据

- (NSUInteger)indexOfObjectAtIndexes:(NSIndexSet *)s options:(NSEnumerationOptions)opts passingTest:(BOOL (NS_NOESCAPE^)(ObjectType obj, NSUInteger idx, BOOL *stop))predicate ;

根据block 的处理获取一个NSIndexSet 对象。 顺序。子数组

- (NSIndexSet *)indexesOfObjectsPassingTest:(BOOL (NS_NOESCAPE ^)(ObjectType obj, NSUInteger idx, BOOL *stop))predicate ;

- (NSIndexSet *)indexesOfObjectsWithOptions:(NSEnumerationOptions)opts passingTest:(BOOL (NS_NOESCAPE ^)(ObjectType obj, NSUInteger idx, BOOL *stop))predicate ;

- (NSIndexSet *)indexesOfObjectsAtIndexes:(NSIndexSet *)s options:(NSEnumerationOptions)opts passingTest:(BOOL (NS_NOESCAPE ^)(ObjectType obj, NSUInteger idx, BOOL *stop))predicate;

    NSIndexSet *index = [array indexesOfObjectsWithOptions:NSEnumerationReverse passingTest: ^ BOOL (id tr, NSUInteger index,BOOL *te){

        NSString *s = (NSString *)tr;
        if([s isEqualToString:@"andy"]){
            return YES;
        }
        return NO;
    }];

    NSLog(@"%@",index);

对数组进行排序操作 参数cmptr 是一个block 函数块,返回的数据类型是一个NSComparisonResult 对象

- (NSArray<ObjectType> *)sortedArrayUsingComparator:(NSComparator NS_NOESCAPE)cmptr;

NSArray *te = [array sortedArrayUsingComparator:^ NSComparisonResult (NSString *s,NSString *s2){
        if(s.length < s2.length){
            return NSOrderedAscending;
        }
        if(s.length > s2.length){
            return NSOrderedDescending;
        }

       return NSOrderedSame;
    }];

    NSLog(@"te=%@.",te);

进行排序操作,NSSortOptions 排序的参数 用来表示是同时排序,还是稳定执行。

- (NSArray<ObjectType> *)sortedArrayWithOptions:(NSSortOptions)opts usingComparator:(NSComparator NS_NOESCAPE)cmptr ;

数组的二分查找法

- (NSUInteger)indexOfObject:(ObjectType)obj inSortedRange:(NSRange)r options:(NSBinarySearchingOptions)opts usingComparator:(NSComparator NS_NOESCAPE)cmp ;

[Objective-C] NSArray的二分查找
http://www.isaced.com/post-241.html

NSMutableArray

交换指定 index1 和 index2 两个位置上的元素

- (void)exchangeObjectAtIndex:(NSUInteger)idx1 withObjectAtIndex:(NSUInteger)idx2;

使用anObject 对象替换 range 位置上的元素,
相当于删除 range位置的元素,然后在把 anobject 插入到这个位置

- (void)removeObject:(ObjectType)anObject inRange:(NSRange)range;

- (void)removeObject:(ObjectType)anObject;

- (void)removeObjectIdenticalTo:(ObjectType)anObject inRange:(NSRange)range;

- (void)removeObjectIdenticalTo:(ObjectType)anObject;

删除一定范围内的所有元素 (不推荐使用)

- (void)removeObjectsFromIndices:(NSUInteger *)indices numIndices:(NSUInteger)cnt;

对当前的数组排序,使用排序算法

- (void)sortUsingFunction:(NSInteger (NS_NOESCAPE *)(ObjectType, ObjectType, void * _Nullable))compare context:(nullable void *)context;

排序

- (void)sortUsingComparator:(NSComparator)cmptr

进行数组排序

- (void)sortWithOptions:(NSSortOptions)opts usingComparator:(NSComparator)cmptrNS_AVAILABLE(10_6,4_0);

相关阅读

[Objective-C] NSArray的二分查找
http://www.isaced.com/post-241.html

IOS之NSArray 中调用的方法详解
http://blog.sina.com.cn/s/blog_702e40a80101gagf.html

NSArray
http://blog.csdn.net/u012973002/article/details/53033493

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值