工具类:防止数组越界崩溃问题(NSArray 分类/runtime 用法3:交换方法)
来源:互联网 发布:智能马桶盖 知乎 编辑:程序博客网 时间:2024/06/06 14:51
#import <Foundation/Foundation.h>@interface NSArray (Safe)@end#import "NSArray+Safe.h"#import <objc/runtime.h>@implementation NSArray (Safe)//这个方法无论如何都会执行+ (void)load { // 选择器 SEL safeSel = @selector(safeObjectAtIndex:); SEL unsafeSel = @selector(objectAtIndex:); Class class = NSClassFromString(@"__NSArrayI"); // 方法 Method safeMethod = class_getInstanceMethod(class, safeSel); Method unsafeMethod = class_getInstanceMethod(class, unsafeSel); // 交换方法 method_exchangeImplementations(unsafeMethod, safeMethod);}- (id)safeObjectAtIndex:(NSUInteger)index { // 数组越界也不会崩,但是开发的时候并不知道数组越界 if (index > (self.count - 1)) { // 数组越界 NSAssert(NO, @"数组越界了"); // 只有开发的时候才会造成程序崩了 return nil; }else { // 没有越界 return [self safeObjectAtIndex:index]; }}@end
1 0
- 工具类:防止数组越界崩溃问题(NSArray 分类/runtime 用法3:交换方法)
- Objective-c防止数组越界而崩溃(全局效果)
- 分类使用runtime交换方法
- iOS runtime运行时特性解决数组越界问题。
- 工具类:快速字典转模型(NSObject 分类/runtime 用法2:获取属性列表)
- Runtime(交换方法)笔记
- 数组越界导致程序崩溃
- iOS中防止数组越界
- 数组越界调试工具
- 排序算法之冒泡排序的基本介绍和数组越界,以及交换2个变量的3种方法
- 一个数组越界问题 数组越界
- UIPickview 省市联动注意点(如何防止同时两组或多组滑动时,数组越界问题)
- byte[] 数组越界,System.arraycopy()方法
- 运行时(runtime)-方法交换
- runtime 方法交换
- runtime 方法交换
- Runtime -- 交换方法
- runtime交换方法
- VMware+Ubuntu+NAT 网络设置
- 驱动函数分类
- Eclipse启动项目时内存溢出问题处理方案
- 在Windows环境下MongoDB搭建和简单操作
- 网络编程socket基本API详解
- 工具类:防止数组越界崩溃问题(NSArray 分类/runtime 用法3:交换方法)
- JVM规范中的五大java内存区域
- hive安装
- Android Intent.FLAG_NEW_TASK详解,包括其他的标记的一些解释
- IRP结构与理解
- 修改linux启动方式
- redis 安装 (新手)
- 浅谈Open vSwitch移植
- poj2738 经典深搜