NSArray custom encapsulation and nsarray custom Encapsulation
NSArray-related data processing is often used in the development process, and it feels complicated to use and highly coupled. The following is a simple encapsulation of NSArray according to your needs.
There are better encapsulation methods available. You are welcome to learn more. Thank you.
NSArray + LSCore. h
//// NSArray + LSCore. h // iPhone /// Created by xujinzhong on 16/8/23. // Copyright 2016 zlll. all rights reserved. // # import <Foundation/Foundation. h> @ interface NSArray (LSCore) # pragma mark-value-(id) firstObject;-(id) randomObject; # pragma mark-sort-(NSArray *) shuffledArray; -(NSArray *) reversedArray;-(NSArray *) uniqueArray;-(NSArray *) arraySorting :( NSString *) parameters ascending :( BOOL) ascending; # pragma mark-security operation-(id) objectWithIndex :( NSUInteger) index;-(NSString *) stringWithIndex :( NSUInteger) index;-(NSNumber *) numberWithIndex :( NSUInteger) index; -(NSArray *) arrayWithIndex :( NSUInteger) index;-(NSDictionary *) dictionaryWithIndex :( NSUInteger) index;-(NSInteger) integerWithIndex :( NSUInteger) index;-(NSUInteger) random :( NSUInteger) index;-(BOOL) rows :( NSUInteger) index;-(int16_t) int16WithIndex :( NSUInteger) index;-(int32_t) int32WithIndex :( NSUInteger) index;-(int64_t) int64WithIndex :( NSUInteger) index;-(char) charWithIndex :( NSUInteger) index;-(short) shortWithIndex :( NSUInteger) index;-(float) floatWithIndex :( NSUInteger) index;-(double) doubleWithIndex :( NSUInteger) index;-(CGFloat) CGFloatWithIndex :( NSUInteger) index;-(CGPoint) pointWithIndex :( NSUInteger) index;-(CGSize) sizeWithIndex :( NSUInteger) index;-(CGRect) rectWithIndex :( NSUInteger) index; @ end
NSArray + LSCore. m
//// NSArray + LSCore. m // iPhone /// Created by xujinzhong on 16/8/23. // Copyright 2016 zlll. all rights reserved. // # import "NSArray + LSCore. h "@ implementation NSArray (LSCore) # pragma mark-value/*** obtain the first element of the array ** @ return element */-(id) firstObject {if ([self count]> 0) {return [self objectAtIndex: 0];} else {return nil ;}} /*** randomly retrieve an element in the array ** @ return random acquisition element */-(id) randomObject {if ([self count]> 0) {int index = arc4random () % [self count]; id object = [self objectAtIndex: index]; return object;} else {return nil ;}} # pragma mark-Sort/*** reorganize the array (disordered) ** @ return the restructured array */-(NSArray *) shuffledArray {NSMutableArray * array = [NSMutableArray arrayWithCapacity: [self count]; NSMutableArray * copy = [self mutableCopy]; while ([copy count]> 0) {int index = arc4random () % [copy count]; id objectToMove = [copy objectAtIndex: index]; [array addObject: objectToMove]; [copy removeObjectAtIndex: index];} return array ;} /*** array Reverse Order ** @ return returns the array in reverse order */-(NSArray *) reversedArray {NSArray * reversedArray = [[self reverseObjectEnumerator] allObjects]; return reversedArray;}/*** the array removes the same elements and obtains a new array ** @ return removes the array */-(NSArray *) after the same elements *) uniqueArray {NSSet * set = [NSSet setWithArray: self]; NSArray * array = [[NSArray alloc] initWithArray: [set allObjects]; return array ;} /*** sort the array content by keywords, return the sorted array ** @ param parameters keyword * @ param ascending YES ascending Order NO descending order ** @ return the sorted array */-(NSArray *) arraySorting :( NSString *) parameters ascending :( BOOL) ascending {parameters * sorter = [[initalloc] initWithKey: parameters ascending: ascending]; optional * sortDescriptors = [externalloc] parameters: & sorter count: 1]; NSArray * sortArray = [self sortedArrayUsingDescriptors: sortDescriptors]; return sortArray ;} # pragma mark-Safe Operation/*** get the object whose subscript is index ** @ param index subscript ** @ return the object obtained */-(id) objectWithIndex :( NSUInteger) index {if (index <self. count) {return self [index];} else {return nil;}/*** obtains the string whose subscript is index. If no null value is returned, it can be converted to string Conversion, cannot return nil ** @ param index subscript ** @ return the string */-(NSString *) stringWithIndex :( NSUInteger) index {id value = [self objectWithIndex: index]; if (value = nil | value = [NSNull null]) {return @ "";} if ([value isKindOfClass: [NSString class]) {return (NSString *) value;} if ([value isKindOfClass: [NSNumber class]) {return [value stringValue];} return nil ;} /*** obtain the number whose subscript is index. If no null value is returned, it can be converted to number conversion, cannot return nil ** @ param index subscript ** @ return the number */-(NSNumber *) numberWithIndex :( NSUInteger) index {id value = [self objectWithIndex: index]; if ([value isKindOfClass: [NSNumber class]) {return (NSNumber *) value;} if ([value isKindOfClass: [NSString class]) {NSNumberFormatter * f = [[NSNumberFormatter alloc] init]; [f setNumberStyle: NSNumberFormatterDecimalStyle]; return [f numberFromString :( NSString *) value];} return nil ;} /*** get the array whose subscript is index, and return nil without saving ** @ param index subscript ** @ return array */-(NSArray *) arrayWithIndex :( NSUInteger) index {id value = [self objectWithIndex: index]; if (value = nil | value = [NSNull null]) {return nil;} if ([value isKindOfClass: [NSArray class]) {return value;} return nil;}/*** obtains the Dictionary with the subscript index, if nil ** @ param index subscript ** is not saved, return the Dictionary */-(NSDictionary *) dictionaryWithIndex (NSUInteger) index {id value = [self objectWithIndex: index]. if (value = nil | value = [NSNull null]) {return nil;} if ([value isKindOfClass: [NSDictionary class]) {return value ;} return nil;}/*** get the integer whose subscript is index. If no value is saved, the return value is 0 ** @ param index subscript ** @ return to the integer */-(NSInteger) integerWithIndex :( NSUInteger) index {id value = [self objectWithIndex: index]; if (value = nil | value = [NSNull null]) {return 0 ;} if ([value isKindOfClass: [NSString class] | [value isKindOfClass: [NSNumber class]) {return [value integerValue];} return 0 ;} /*** get the unsignedInteger whose subscript is index. If no value is saved, the return value is 0 ** @ param index subscript ** @ return to unsignedInteger */-(NSUInteger) unsignedIntegerWithIndex :( NSUInteger) index {id value = [self objectWithIndex: index]; if (value = nil | value = [NSNull null]) {return 0;} if ([value isKindOfClass: [NSString class] | [value isKindOfClass: [NSNumber class]) {return [value unsignedIntegerValue];} return 0;}/*** get the bool with the index subscript, if NO value is saved, return the NO ** @ param index subscript ** @ return bool */-(BOOL) boolWithIndex :( NSUInteger) index {id value = [self objectWithIndex: index]; if (value = nil | value = [NSNull null]) {return NO;} if ([value isKindOfClass: [NSNumber class]) {return [value boolValue];} if ([value isKindOfClass: [NSString class]) {return [value boolValue];} return NO ;} /*** get int16 whose subscript is index. If no value is saved, the system returns 0 ** @ param index subscript ** @ return to get int16 */-(int16_t) int16WithIndex :( NSUInteger) index {id value = [self objectWithIndex: index]; if (value = nil | value = [NSNull null]) {return 0;} if ([value isKindOfClass: [NSNumber class]) {return [value pair value];} if ([value isKindOfClass: [NSString class]) {return [value intValue];} return 0 ;} /*** get int32 whose subscript is index. If no value is saved, return 0 *** @ param index subscript ** @ return to get int32 */-(int32_t) int32WithIndex :( NSUInteger) index {id value = [self objectWithIndex: index]; if (value = nil | value = [NSNull null]) {return 0;} if ([value isKindOfClass: [NSNumber class] | [value isKindOfClass: [NSString class]) {return [value intValue];} return 0;}/*** get int64 whose subscript is index, if no value is saved, the system returns the int64 */-(int64_t) int64WithIndex (NSUInteger) index {id value = [self objectWithIndex: index] obtained by @ param index subscript ** @ return. if (value = nil | value = [NSNull null]) {return 0;} if ([value isKindOfClass: [NSNumber class] | [value isKindOfClass: [NSString class]) {return [value longLongValue];} return 0;}/*** get charValue with the subscript index, if no value is saved, the returned charValue */-(char) charWithIndex (NSUInteger) index {id value = [self objectWithIndex: index] is 0 ** @ param index subscript ** @ return. if (value = nil | value = [NSNull null]) {return 0;} if ([value isKindOfClass: [NSNumber class] | [value isKindOfClass: [NSString class]) {return [value charValue];} return 0;}/*** get the intValue whose subscript is index, if no value is saved, return the intValue */-(short) shortWithIndex (NSUInteger) index {id value = [self objectWithIndex: index] obtained by @ param index subscript ** @ return. if (value = nil | value = [NSNull null]) {return 0;} if ([value isKindOfClass: [NSNumber class]) {return [value struct value];} if ([value isKindOfClass: [NSString class]) {return [value intValue];} return 0 ;} /*** get the floatValue whose subscript is index. If no value is saved, return 0 ** @ param index subscript ** @ return to obtain the floatValue */-(float) floatWithIndex :( NSUInteger) index {id value = [self objectWithIndex: index]; if (value = nil | value = [NSNull null]) {return 0;} if ([value isKindOfClass: [NSNumber class] | [value isKindOfClass: [NSString class]) {return [value floatValue];} return 0;}/*** get doubleValue with the subscript index, if no value is saved, return the doubleValue */-(double) doubleWithIndex (NSUInteger) index {id value = [self objectWithIndex: index] obtained by @ param index subscript ** @ return. if (value = nil | value = [NSNull null]) {return 0;} if ([value isKindOfClass: [NSNumber class] | [value isKindOfClass: [NSString class]) {return [value doubleValue];} return 0 ;} /*** get CGFloat whose subscript is index ** @ param index subscript ** @ return get CGFloat */-(CGFloat) CGFloatWithIndex :( NSUInteger) index {id value = [self objectWithIndex: index]; CGFloat f = [value doubleValue]; return f ;} /*** get the point whose subscript is index ** @ param index subscript ** @ return the point */-(CGPoint) pointWithIndex :( NSUInteger) index {id value = [self objectWithIndex: index]; CGPoint point = CGPointFromString (value); return point ;} /*** get the size of index with the subscript *** @ param index subscript ** @ return returns the size */-(CGSize) sizeWithIndex :( NSUInteger) index {id value = [self objectWithIndex: index]; CGSize size = CGSizeFromString (value); return size ;} /*** obtain the rect ** @ param index subscript ** @ return the rect */-(CGRect) rectWithIndex :( NSUInteger) index {id value = [self objectWithIndex: index]; CGRect rect = CGRectFromString (value); return rect ;}@ end