TPreventKVC
Features
Use the runtime dynamic replacement method to prevent NSUnknownKeyException & NSInvalidArgumentException errors from crashing when using KVC's methods.
-valueForKey:setValue:forKey:-setValue:forKeyPath:-valueForKeyPath:, etc.
Installation
Source File
Drag all the files inside the TPreventKVC folder in the corresponding module directory into you project.
CocoaPods
CocoaPods is a dependency manager for Cocoa projects. You can install it with the following command:
$ gem install cocoapodsTo integrate TPreventKVC into your Xcode project using CocoaPods, specify it in your Podfile:
pod 'TPreventKVC'Then, run the following command:
$ pod installCarthage
Carthage is a decentralized dependency manager that builds your dependencies and provides you with binary frameworks.
You can install Carthage with Homebrew using the following command:
$ brew update
$ brew install carthageTo integrate TPreventKVC into your Xcode project using Carthage, specify it in your Cartfile:
github "tobedefined/TPreventKVC"Run carthage update to build the framework and drag the built TPreventKVC.framework into your Xcode project.
How to use
After importing the project.
Set Block Get Error Infomation
In the main() function of the APP's main.m file or in the APP's didFinishLaunching method add the following code to get the specific information about the missing method:
[NSObject setHandleKVCErrorBlock:^(__unsafe_unretained Class cls, NSString *key, KVCErrorType errorType) {
// DO SOMETHING
// like upload to server or print log or others
}];一些定义
The following definitions and methods are in 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:Classtype; useNSStringFromClass(cls)to return a class name string for a Class that produces the wrong class or objectkey:NSString *type; the key to generate the errorerrorType:KVCErrorType; the type that generated the errorcallStackSymbols:NSArray<NSString *> *type; call stack infomations