项目作者: ToBeDefined

项目描述 :
Use the runtime dynamic replacement method to prevent NSUnknownKeyException & NSInvalidArgumentException errors from crashing when using KVC's methods.
高级语言: Objective-C
项目地址: git://github.com/ToBeDefined/TPreventKVC.git
创建时间: 2018-04-02T10:51:55Z
项目社区:https://github.com/ToBeDefined/TPreventKVC

开源协议:MIT License

下载




TPreventKVC
———



platform
Carthage compatible
CocoaPods
Build Status
License MIT

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:

  1. $ gem install cocoapods

To integrate TPreventKVC into your Xcode project using CocoaPods, specify it in your Podfile:

  1. pod 'TPreventKVC'

Then, run the following command:

  1. $ pod install

Carthage

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:

  1. $ brew update
  2. $ brew install carthage

To integrate TPreventKVC into your Xcode project using Carthage, specify it in your Cartfile:

  1. 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:

  1. [NSObject setHandleKVCErrorBlock:^(__unsafe_unretained Class cls, NSString *key, KVCErrorType errorType) {
  2. // DO SOMETHING
  3. // like upload to server or print log or others
  4. }];
一些定义

The following definitions and methods are in NSObject+PreventKVC.h

  1. typedef NS_ENUM(NSUInteger, KVCErrorType) {
  2. KVCErrorTypeSetValueForUndefinedKey = 1,
  3. KVCErrorTypeValueForUndefinedKey = 2,
  4. KVCErrorTypeSetNilValueForKey = 3,
  5. };
  6. typedef void (^ __nullable HandleKVCErrorBlock)(Class cls, NSString *key, KVCErrorType errorType);
  • cls: Class type; use NSStringFromClass(cls) to return a class name string for a Class that produces the wrong class or object
  • key: NSString * type; the key to generate the error
  • errorType: KVCErrorType; the type that generated the error
  • callStackSymbols: NSArray<NSString *> * type; call stack infomations