NSArray 自訂封裝,nsarray自訂封裝

來源:互聯網
上載者:User

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

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.