iOS - 基础集合类

xiaoxiao2021-02-28  33

iOS基础集合类

NSArray

排序

- (NSEnumerator<ObjectType> *)objectEnumerator; - (NSEnumerator<ObjectType> *)reverseObjectEnumerator; - (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<ObjectType> *)sortedArrayUsingSelector:(SEL)comparator; - (NSArray<ObjectType> *)subarrayWithRange:(NSRange)range;

1. 逆序

/// 将原有的数组逆序输出 [[array reverseObjectEnumerator] allObjects]; - (void)testArraySort { NSArray *array1 = @[@1,@123,@98,@6,@77,@22]; NSArray *result = [[array1 reverseObjectEnumerator] allObjects]; NSLog(@"%@",result); }

输出:

2018-05-11 10:32:21.820441+0800 XWArrayDictionaryDemo[2148:94679] ( 22, 77, 6, 98, 123, 1 )

2. 升序 、降序

升序
/// 升序 - (void)testNumberSort { NSArray *array = @[@1,@123,@98,@6,@77,@22]; NSArray *result = [array sortedArrayUsingSelector:@selector(compare:)]; NSLog(@"%@",result); }

输出:

2018-05-11 13:52:31.889127+0800 XWArrayDictionaryDemo[5877:230244] ( 1, 6, 22, 77, 98, 123 ) /// 升序 - (void)testNumberSort2 { NSArray *array = @[@1,@9,@6,@2,@7,@4]; NSArray *result = [array sortedArrayUsingComparator:^NSComparisonResult(id _Nonnull obj1, id _Nonnull obj2) { return [obj1 compare:obj2]; }]; NSLog(@"%@",result); }

输出:

2018-05-11 13:58:59.779001+0800 XWArrayDictionaryDemo[6065:252727] ( 1, 2, 4, 6, 7, 9 )
降序
/// 降序 - (void)testSort3 { NSArray *array = @[@1,@9,@6,@2,@7,@4]; NSArray *result = [[[array sortedArrayUsingSelector:@selector(compare:)] reverseObjectEnumerator] allObjects]; NSLog(@"%@",result); }

输出:

2018-05-11 14:01:36.237543+0800 XWArrayDictionaryDemo[6140:260610] ( 9, 7, 6, 4, 2, 1 ) /// 降序 - (void)testSort4 { NSArray *array = @[@1,@9,@6,@2,@7,@4]; NSArray *result = [array sortedArrayUsingComparator:^NSComparisonResult(id _Nonnull obj1, id _Nonnull obj2) { return [obj2 compare:obj1]; }]; NSLog(@"%@",result); }

输出:

2018-05-11 14:02:41.385379+0800 XWArrayDictionaryDemo[6182:265228] ( 9, 7, 6, 4, 2, 1 )

枚举

使用 indexesOfObjectsWithOptions:passingTest:

- (void)testEnum { NSArray *array = @[@"邱学伟",@"极客学伟",@"一米八",@"",@"iOS开发"]; [array indexesOfObjectsWithOptions:NSEnumerationConcurrent passingTest:^BOOL(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { NSLog(@"%@",obj); return YES; }]; }

For 循环

- (void)testEnum1 { NSArray *array = @[@"邱学伟",@"极客学伟",@"一米八",@"",@"iOS开发"]; for (NSString *str in array) { NSLog(@"%@",str); } }

Block 方式枚举

- (void)testEnum1 { NSArray *array = @[@"邱学伟",@"极客学伟",@"一米八",@"",@"iOS开发"]; [array enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { NSLog(@"%@",obj); }]; }

通过下标 index

- (void)testEnum2 { NSArray *array = @[@"邱学伟",@"极客学伟",@"一米八",@"",@"iOS开发"]; for (int i = 0 ; i < array.count; i++) { NSLog(@"%@",array[i]); } }

使用 NSEnumerator

- (void)testEnum3 { NSArray *array = @[@"邱学伟",@"极客学伟",@"一米八",@"",@"iOS开发"]; NSEnumerator *enumerator = [array objectEnumerator]; id obj; while (obj = [enumerator nextObject]) { NSLog(@"%@",obj); } }

使用 predicate

- (void)testEnum4 { NSArray *array = @[@"邱学伟",@"极客学伟",@"一米八",@"",@"iOS开发"];\ [array filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(id _Nullable evaluatedObject, NSDictionary<NSString *,id> * _Nullable bindings) { NSLog(@"%@",evaluatedObject); return YES; }]]; } 枚举方法 / 时间 [ms]10.000.000 elements10.000 elementsindexesOfObjects:, concurrent1844.732.25NSFastEnumeration (for in)3223.453.21indexesOfObjects:4221.233.36enumerateObjectsUsingBlock:5459.435.43objectAtIndex:5282.675.53NSEnumerator5566.925.75filteredArrayUsingPredicate:6466.956.31

NSDictionary

同样数目的值,NSDictionary比NSArray要花费多得多的内存空间

排序

通过 value 的顺序将 key 按指定顺序输出

- (NSArray *)keysSortedByValueUsingSelector:(SEL)comparator; - (NSArray *)keysSortedByValueUsingComparator:(NSComparator)cmptr; - (NSArray *)keysSortedByValueWithOptions:(NSSortOptions)opts usingComparator:(NSComparator)cmptr;

枚举

keysOfEntriesWithOptions:passingTest: 可并行

- (void)testDictEnum { NSDictionary *dict = @{@"邱学伟":@"3",@"极客学伟":@"1",@"一米八":@"2"}; [dict keysOfEntriesWithOptions:NSEnumerationConcurrent passingTest:^BOOL(id _Nonnull key, id _Nonnull obj, BOOL * _Nonnull stop) { NSLog(@"key : %@, value : %@",key,obj); return YES; }]; }

block 枚举

- (void)testDictEnum2 { NSDictionary *dict = @{@"邱学伟":@"3",@"极客学伟":@"1",@"一米八":@"2"}; [dict enumerateKeysAndObjectsUsingBlock:^(id _Nonnull key, id _Nonnull obj, BOOL * _Nonnull stop) { NSLog(@"key : %@, value : %@",key,obj); }]; } 各个方法枚举时间参考 枚举方法 / 时间 [ms]50.000 elements1.000.000 elementskeysOfEntriesWithOptions:, concurrent16.65425.24getObjects:andKeys:30.33798.49keysOfEntriesWithOptions:30.59856.93enumerateKeysAndObjectsUsingBlock:36.33882.93NSFastEnumeration41.201043.42NSEnumeration42.211113.08
转载请注明原文地址: https://www.6miu.com/read-1950324.html

最新回复(0)