TPreventUnrecognizedSEL 1.1.1

TPreventUnrecognizedSEL 1.1.1

测试已测试
语言语言 Obj-CObjective C
许可证 MIT
发布最后发布2018年4月

ToBeDefined 维护。



TPreventUnrecognizedSEL

platform  Carthage compatible  CocoaPods  Build Status  License MIT

特性

  • 使用运行时动态添加方法以防止出现 未识别的选择器 错误,防止应用程序因缺少对象和类方法而导致崩溃。

    实例方法:*** 程序因未捕获的异常 'NSInvalidArgumentException' 停止运行,原因是 '-[TestClass losted:instance:method:]: 向实例 0x102c.... 发送了未识别的选择器'

    类方法:*** 程序因未捕获的异常 'NSInvalidArgumentException' 停止运行,原因是 '+[TestClass losted:class:method:]: 向类 0x10000.... 发送了未识别的选择器'

  • 可以获取丢失方法的具体信息,包括

    • 丢失的类方法或对象方法的类名;
    • 丢失方法的名称;
    • 丢失的对象方法或类方法。

安装

源文件

源文件包含两个模块目录:TPUSELNormalForwardingTPUSELFastForwarding;将对应模块目录下的 Sources 文件夹中的所有文件拖入您的项目中。

⚠️注意:您只能使用其中模块,可以拖入项目中的是所有源文件对应的模块目录。建议使用 TPUSELNormalForwarding,因为系统中一些方法的实现使用了快速跳转。

CocoaPods

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

$ gem install cocoapods

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

# (pod 'TPreventUnrecognizedSEL') default is use (pod 'TPreventUnrecognizedSEL/NormalForwarding')
pod 'TPreventUnrecognizedSEL/NormalForwarding'

或这

pod 'TPreventUnrecognizedSEL/FastForwarding'

然后,运行以下命令

$ pod install

⚠️注意:您只能使用其中的一个 subspec,即 NormalForwardingFastForwarding

⚠️使用 pod 'TPreventUnrecognizedSEL' 默认是 pod 'TPreventUnrecognizedSEL/NormalForwarding'

Carthage

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

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

$ brew update
$ brew install carthage

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

github "tobedefined/TPreventUnrecognizedSEL"

运行 carthage update 以构建框架,并将构建好的 TPUSELNormalForwarding.frameworkTPUSELFastForwarding.framework 拖入您的 Xcode 项目中。

⚠️注意:您只能使用这两个框架中的一个,即 TPUSELNormalForwarding.frameworkTPUSELFastForwarding.framework

如何使用

简单使用

项目导入后,只需简单地设置要转发哪些类。

获取运行错误消息

导入头文件
模块和语言 \ 导入模块模式 源文件 CocoaPods Carthage
TPUSELNormalForwarding & ObjC #import "TPUSELNormalForwarding.h" #import <TPreventUnrecognizedSEL/TPUSELNormalForwarding.h> #import <TPUSELNormalForwarding/TPUSELNormalForwarding.h>
TPUSELNormalForwarding & Swift 在 Bridging-Header 中添加 ⤴ import TPreventUnrecognizedSEL import TPUSELNormalForwarding
TPUSELFastForwarding & ObjC #import "TPUSELFastForwarding.h" #import <TPreventUnrecognizedSEL/TPUSELFastForwarding.h> #import <TPUSELFastForwarding/TPUSELFastForwarding.h>
TPUSELFastForwarding & Swift 在 Bridging-Header 中添加 ⤴ import TPreventUnrecognizedSEL import TPUSELFastForwarding
设置转发和块

在 APP 的 main.m 文件的 main() 函数在 APP 的 didFinishLaunching 方法中 添加以下代码以获取关于缺失方法的详细信息:

TPUSELNormalForwarding
// Setting does not process NSNull and its subclasses, the default is NO
[NSObject setIgnoreForwardNSNullClass:YES];
// Set the class and its subclasses in the array to not be processed (the class name in the array can be a Class type or an NSString type). By default, no class is ignored.
[NSObject setIgnoreForwardClassArray:@[@"SomeIgnoreClass1", [SomeIgnoreClass2 Class]]];
// Set to process only the classes in the array and their subclasses (the class name in the array can be either a Class or an NSString type). By default, all classes are handled.
[NSObject setJustForwardClassArray:@[@"SomeClass1", [SomeClass2 Class]]];
// Set callback after processing
[NSObject setHandleUnrecognizedSELErrorBlock:^(Class  _Nonnull __unsafe_unretained cls, SEL  _Nonnull selector, UnrecognizedMethodType methodType, NSArray<NSString *> * _Nonnull callStackSymbols) {
    // DO SOMETHING
    // like upload to server or print log or others
}];
TPUSELFastForwarding
// Set to process only the classes in the array and their subclasses (the class name in the array can be either a Class or an NSString type). By default, all classes are NOT handled.
// Set callback after processing
[NSObject setJustForwardClassArray:@[@"SomeClass1", [SomeClass2 Class]]
   handleUnrecognizedSELErrorBlock:^(Class  _Nonnull __unsafe_unretained cls, SEL  _Nonnull selector, UnrecognizedMethodType methodType, NSArray<NSString *> * _Nonnull callStackSymbols) {
       // DO SOMETHING
       // like upload to server or print log or others
   }];
一些定义

以下定义和方法位于 NSObject+TPUSELFastForwarding.hNSObject+TPUSELNormalForwarding.h

typedef NS_ENUM(NSUInteger, UnrecognizedMethodType) {
    UnrecognizedMethodTypeClassMethod       = 1,
    UnrecognizedMethodTypeInstanceMethod    = 2,
};

typedef void (^ __nullable HandleUnrecognizedSELErrorBlock)(Class cls,
                                                            SEL selector,
                                                            UnrecognizedMethodType methodType,
                                                            NSArray<NSString *> *callStackSymbols);
  • cls: Class 类型; 缺失的实例方法或类方法所属的类,可以使用 NSStringFromClass(cls) 返回类名称的 NSString
  • selector: SEL 类型; 缺失的方法名称,可以使用 NSStringFromSelector(selector) 返回方法名称的 NSString
  • methodType: UnrecognizedMethodType 类型; 对于缺失的方法类型(类方法或对象方法)
  • callStackSymbols: NSArray