NSArray 自訂封裝,nsarray自訂封裝
在開發過程中經常會用到NSArray相關的資料處理,感覺用起來比較繁瑣和耦合性比較高。以下根據自己的需求對NSArray做了一下簡單的封裝。
有更好的封裝方法,歡迎大家提出學習。謝謝
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 - 取值- (id)firstObject;- (id)randomObject;#pragma mark - 排序- (NSArray *)shuffledArray;- (NSArray *)reversedArray;- (NSArray *)uniqueArray;- (NSArray *)arraySorting:(NSString *)parameters ascending:(BOOL)ascending;#pragma mark - 安全操作-(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)unsignedIntegerWithIndex:(NSUInteger)index;- (BOOL)boolWithIndex:(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 - 取值/** * 擷取數組的第一個元素 * * @return 取得的元素 */- (id)firstObject { if ([self count] > 0) { return [self objectAtIndex:0]; } else { return nil; }}/** * 隨機取數組裡的一個元素 * * @return 隨機取得的元素 */- (id)randomObject { if ([self count] > 0) { int index = arc4random() % [self count]; id object = [self objectAtIndex:index]; return object; } else { return nil; }}#pragma mark - 排序/** * 重組數組(打亂順序) * * @return 重組之後的數組 */- (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;}/** * 數組倒序 * * @return 將數組倒序後得到的數組 */- (NSArray *)reversedArray { NSArray *reversedArray = [[self reverseObjectEnumerator] allObjects]; return reversedArray;}/** * 數組去除相同的元素,並獲得新的array * * @return 去除相同元素後的array */- (NSArray *)uniqueArray { NSSet *set = [NSSet setWithArray:self]; NSArray *array = [[NSArray alloc] initWithArray:[set allObjects]]; return array;}/** * 根據關鍵詞 對array的內容進行排序,並返回排序後的array * * @param parameters 關鍵詞 * @param ascending YES 升序 NO 降序 * * @return 經過排序後的數組 */- (NSArray *)arraySorting:(NSString *)parameters ascending:(BOOL)ascending { NSSortDescriptor*sorter=[[NSSortDescriptor alloc]initWithKey:parameters ascending:ascending]; NSMutableArray *sortDescriptors=[[NSMutableArray alloc]initWithObjects:&sorter count:1]; NSArray *sortArray=[self sortedArrayUsingDescriptors:sortDescriptors]; return sortArray;}#pragma mark - 安全操作/** * 擷取下標為index的對象 * * @param index 下標 * * @return 獲得的對象 */-(id)objectWithIndex:(NSUInteger)index { if (index <self.count) { return self[index]; }else{ return nil; }}/** * 擷取下標為index的字串,不存在返回空,能轉為string的轉,不能的返回nil * * @param index 下標 * * @return 獲得的字串 */- (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;}/** * 擷取下標為index的number,不存在返回空,能轉為number的轉,不能的返回nil * * @param index 下標 * * @return 獲得的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;}/** * 擷取下標為index的array,不存返回nil * * @param index 下標 * * @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;}/** * 擷取下標為index的Dictionary,不存返回nil * * @param index 下標 * * @return 獲得的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;}/** * 擷取下標為index的integer,不存返回0 * * @param index 下標 * * @return 獲得的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;}/** * 擷取下標為index的unsignedInteger,不存返回0 * * @param index 下標 * * @return 獲得的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;}/** * 擷取下標為index的bool,不存返回NO * * @param index 下標 * * @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;}/** * 擷取下標為index的int16,不存返回0 * * @param index 下標 * * @return 獲得的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 shortValue]; } if ([value isKindOfClass:[NSString class]]) { return [value intValue]; } return 0;}/** * 擷取下標為index的int32,不存返回0 * * @param index 下標 * * @return 獲得的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;}/** * 擷取下標為index的int64,不存返回0 * * @param index 下標 * * @return 獲得的int64 */- (int64_t)int64WithIndex:(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 longLongValue]; } return 0;}/** * 擷取下標為index的charValue,不存返回0 * * @param index 下標 * * @return 獲得的charValue */- (char)charWithIndex:(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 charValue]; } return 0;}/** * 擷取下標為index的intValue,不存返回0 * * @param index 下標 * * @return 獲得的intValue */- (short)shortWithIndex:(NSUInteger)index { id value = [self objectWithIndex:index]; if (value == nil || value == [NSNull null]) { return 0; } if ([value isKindOfClass:[NSNumber class]]) { return [value shortValue]; } if ([value isKindOfClass:[NSString class]]) { return [value intValue]; } return 0;}/** * 擷取下標為index的floatValue,不存返回0 * * @param index 下標 * * @return 獲得的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;}/** * 擷取下標為index的doubleValue,不存返回0 * * @param index 下標 * * @return 獲得的doubleValue */- (double)doubleWithIndex:(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 doubleValue]; } return 0;}/** * 擷取下標為index的CGFloat * * @param index 下標 * * @return 獲得的CGFloat */- (CGFloat)CGFloatWithIndex:(NSUInteger)index { id value = [self objectWithIndex:index]; CGFloat f = [value doubleValue]; return f;}/** * 擷取下標為index的point * * @param index 下標 * * @return 獲得的point */- (CGPoint)pointWithIndex:(NSUInteger)index { id value = [self objectWithIndex:index]; CGPoint point = CGPointFromString(value); return point;}/** * 擷取下標為index的size * * @param index 下標 * * @return 獲得的size */- (CGSize)sizeWithIndex:(NSUInteger)index { id value = [self objectWithIndex:index]; CGSize size = CGSizeFromString(value); return size;}/** * 擷取下標為index的rect * * @param index 下標 * * @return 獲得的rect */- (CGRect)rectWithIndex:(NSUInteger)index { id value = [self objectWithIndex:index]; CGRect rect = CGRectFromString(value); return rect;}@end