TPreventKVC
特性
使用运行时动态替换方法防止使用KVC的方法时出现的NSUnknownKeyException
和NSInvalidArgumentException
错误导致崩溃。
-valueForKey
setValue:forKey
-setValue:forKeyPath
-valueForKeyPath:
等。
安装
源文件
将
CocoaPods
CocoaPods
是Cocoa项目的依赖关系管理器。您可以使用以下命令安装它
$ gem install cocoapods
要使用CocoaPods将Podfile
中指定它
pod 'TPreventKVC'
然后,运行以下命令
$ pod install
Carthage
Carthage
是一个去中心化的依赖关系管理器,它构建您的依赖项并提供二进制框架。
您可以使用以下命令使用 Homebrew
安装Carthage
$ brew update
$ brew install carthage
要使用Carthage将Cartfile
中指定它
github "tobedefined/TPreventKVC"
运行carthage update
以构建框架,并将构建的
如何使用
导入项目后。
设置阻塞获取错误信息
在APP的main.m
文件的main()
函数或
在APP的didFinishLaunching
方法中
添加以下代码以获取有关缺失方法的详细信息:
[NSObject setHandleKVCErrorBlock:^(__unsafe_unretained Class cls, NSString *key, KVCErrorType errorType) {
// DO SOMETHING
// like upload to server or print log or others
}];
一些定义
以下定义和方法位于NSObject+PreventKVC.h
中:
typedef NS_ENUM(NSUInteger, KVCErrorType) {
KVCErrorTypeSetValueForUndefinedKey = 1,
KVCErrorTypeValueForUndefinedKey = 2,
KVCErrorTypeSetNilValueForKey = 3,
};
typedef void (^ __nullable HandleKVCErrorBlock)(Class cls, NSString *key, KVCErrorType errorType);
cls
:Class
类型;使用NSStringFromClass(cls)
为产生错误类或对象的技术生成类名字符串key
:NSString *
类型;产生错误的密钥errorType
:KVCErrorType
;产生错误的类型callStackSymbols
:NSArray
类型;调用堆栈信息