Gets an object in an array

// @param index = positive left -> right, negative left < -right - (id)exp_objAtIndex:(NSInteger)index;Copy the code
Example:
NSArray *arry = @[@"1",@"2",@"3"]; // left -> right NSString * str0 = [arry exp_objAtIndex:0]; NSString * str1 = [arry exp_objAtIndex:1]; NSString * str2 = [arry exp_objAtIndex:2]; NSLog(@"====%@===%@====%@",str0,str1,str2); // left < -right NSString * str_1 = [arry exp_objAtIndex:-1]; NSString * str_2 = [arry exp_objAtIndex:-2]; NSString * str_3 = [arry exp_objAtIndex:-3]; NSLog(@"====%@===%@====%@",str_1,str_2,str_3);Copy the code
Running results:

Whether the array is empty

/// whether the array isEmpty - (BOOL)isEmpty;Copy the code
Example:
NSArray *arry = @[@"1",@"2",@"3"]; If (arry.isEmpty) {NSLog(@" empty "); }else{NSLog(@" value "); }Copy the code
Running results:

An array of map

- (NSArray *)mapper:(id(^)(id obj))mapper;Copy the code
Example:
    NSArray *arry = @[@"1",@"2",@"3"];
   NSArray *mapperArray = [arry mapper:^id _Nonnull(NSString * obj) {
        return [obj stringByAppendingString:@"_1"];
    }];
    NSLog(@"======%@",mapperArray);
Copy the code
Running results:

An array of screening

@param filter - (NSArray *)filter:(BOOL(^)(id obj))filter;Copy the code
Example:
  NSArray *arry = @[@"1",@"2",@"3"];
    NSArray *filterArray = [arry filter:^BOOL(NSString * obj) {
        return  [obj isEqualToString:@"2"];
    }];
    NSLog(@"======%@",filterArray);
Copy the code
Running results:

Array element concatenation

/// join array elements -(NSString *(^)(NSString *separator))joinedByString;Copy the code
Example:
   NSArray *arry = @[@"1",@"2",@"3"];
  NSString *str =   arry.joinedByString(@",");
    NSLog(@"======%@",str);
Copy the code
Running results:

The source code

.h
// // NSArray+ expand. h // Array extension // // Created by Zhiwei Cui on 2020/10/13. // Copyright © 2020 Zhiwei Cui. All rights reserved. // #import <Foundation/Foundation.h> NS_ASSUME_NONNULL_BEGIN @interface NSArray (Expand) /// // @param index = positive left -> right, negative left < -right - (id)exp_objAtIndex:(NSInteger)index; /// whether the array isEmpty - (BOOL)isEmpty; - (NSArray *)mapper:(id(^)(id obj))mapper; @param filter - (NSArray *)filter:(BOOL(^)(id obj))filter; /// join array elements -(NSString *(^)(NSString *separator))joinedByString; @end NS_ASSUME_NONNULL_ENDCopy the code
.m
// // NSArray+ expand. m // array extension // // Created by Zhiwei Cui on 2020/10/13. // Copyright © 2020 Zhiwei Cui. All rights reserved. // #import "NSArray+Expand.h" @implementation NSArray (Expand) - (id)exp_objAtIndex:(NSInteger)index{ if (index >=0) { return [self objectAtIndex:index]; }else{ return [self objectAtIndex:self.count +index]; } } - (BOOL)isEmpty{ return self.count<=0; } - (NSArray*)mapper:(id(^)(id obj))mapper{ NSMutableArray *array = [NSMutableArray array]; for (id item in self) { if (mapper) { id newItem = mapper(item); [array addObject:newItem]; } } return array.copy; } - (NSArray*)filter:(BOOL(^)(id obj))filter{ NSMutableArray *array = [NSMutableArray array]; for (id item in self) { if (filter) { BOOL isFit = filter(item); if (isFit) { [array addObject:item]; } } } return array.copy; } -(NSString *(^)(NSString *separator))joinedByString{ return ^(NSString *separator) { return [self componentsJoinedByString:separator]; }; } @endCopy the code