TPreventKVC 1.1.1

TPreventKVC 1.1.1

ToBeDefined维护。



TPreventKVC

platform  Carthage compatible  CocoaPods  Build Status  License MIT

特性

使用运行时动态替换方法防止使用KVC的方法时出现的NSUnknownKeyExceptionNSInvalidArgumentException错误导致崩溃。

  • -valueForKey
  • setValue:forKey
  • -setValue:forKeyPath
  • -valueForKeyPath:等。

安装

源文件

文件夹内的所有文件拖动到相应的模块目录下的项目中。

CocoaPods

CocoaPods 是Cocoa项目的依赖关系管理器。您可以使用以下命令安装它

$ gem install cocoapods

要使用CocoaPods将集成到您的Xcode项目中,请在您的Podfile中指定它

pod 'TPreventKVC'

然后,运行以下命令

$ pod install

Carthage

Carthage 是一个去中心化的依赖关系管理器,它构建您的依赖项并提供二进制框架。

您可以使用以下命令使用 Homebrew 安装Carthage

$ brew update
$ brew install carthage

要使用Carthage将集成到您的Xcode项目中,请在您的Cartfile中指定它

github "tobedefined/TPreventKVC"

运行carthage update以构建框架,并将构建的拖动到您的Xcode项目中。

如何使用

导入项目后。

设置阻塞获取错误信息

在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);
  • clsClass类型;使用NSStringFromClass(cls)为产生错误类或对象的技术生成类名字符串
  • keyNSString *类型;产生错误的密钥
  • errorTypeKVCErrorType;产生错误的类型
  • callStackSymbolsNSArray类型;调用堆栈信息