iOS 防止数组越界的解决方法

代码:

#import <Foundation/Foundation.h>

//防止数据越界
@interface NSArray (DXIndexCheckArray)

- (id)objectAtIndexCheck:(NSUInteger)index;

@end
#import "NSArray+DXIndexCheckArray.h"

@implementation NSArray (DXIndexCheckArray)

- (id)objectAtIndexCheck:(NSUInteger)index{
    if (index > self.count) {
        return  nil;
    }
    
    id value = [self objectAtIndex:index];
    if (value == [NSNull null]) {
        return  nil;
    }
    return value;
}

@end

 

posted @ 2016-07-06 15:51  Crazy_ZY  阅读(622)  评论(0编辑  收藏  举报