這樣包裝一下Array是不是會好一點

//
// NSArray+beyond.h
// testRetainCount
//
// Created by jie on 16/5/10.
// Copyright ? 2016年 jie. All rights reserved.
//

import <Foundation/Foundation.h>

@interface NSArray (beyond)
-(id)achieveSafeObjectAtIndex:(NSInteger)index;
@end

//
// NSArray+beyond.m
// testRetainCount
//
// Created by jie on 16/5/10.
// Copyright ? 2016年 jie. All rights reserved.
//

import "NSArray+beyond.h"

@implementation NSArray (beyond)
-(id)achieveSafeObjectAtIndex:(NSInteger)index{
if (index >= 0 && index < self.count) {
id obj = [self objectAtIndex:index];
return obj;
}else{
NSAssert(index < self.count, @"數組越界");
}
return nil;
}
@end

NSArray *array = @[@"1",@"2",@"3",@"4"];
id obj =  [array achieveSafeObjectAtIndex:1];
NSLog(@"%@",obj);
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容