OCFuntime 是一个针对 Objective-C 运行时的工具包。
方法实现更改允许在相应的方法调用上运行代码块。不要与 方法交换 混淆。
实例方法实现代码块应遵守签名
method_return_type ^(id selfInstance, arg1_type arg1, arg2_type arg2, ...)
类方法实现代码块应遵守签名
method_return_type ^(Class theClass, arg1_type arg1, arg2_type arg2, ...)
安装
将 pod 'OCFuntime/Methods'
添加到 Podfile
示例
@interface MyClass : NSObject
- (void)someInstanceMethod:(NSObject *)someArg;
+ (NSInteger)someClassMethod;
@end
#import "OCFuntime+Methods.h"
...
OCFuntime *funtime = [[OCFuntime alloc] init];
// Change instance method implementation
[funtime changeClass:MyClass.class instanceMethod:@selector(someInstanceMethod:)
implementation:^(MyClass *instance, NSObject *someArg)
{
NSLog(@"Changed instance method with arg %@!", someArg);
}];
// Change class method implementation
[funtime changeClass:MyClass.class classMethod:@selector(someClassMethod)
implementation:^(Class theClass)
{
NSLog(@"Changed class method of %@!", NSStringFromClass(theClass));
return 5;
}];
//Revert method to default implementation
[funtime revertClass:MyClass.class instanceMethod:@selector(someInstanceMethod)];
[funtime revertClass:MyClass.class classMethod:@selector(someClassMethod)];
// Revert all methods of class to default implementation
[funtime revertClassMethods:MyClass.class];
// Revert all changed methods to default implementation
[funtime revertAllMethods];
注意
OCFuntime
的 共享实例。NSObject 分类
使用 NSObject
分类在相应的类上运行方法实现更改。
将 pod 'OCFuntime/NSObject+OCFMethods'
添加到 Podfile
#import "NSObject+OCFMethods.h"
...
// Change class method implementation
[MyClass changeClassMethod:@selector(someClassMethod) implementation:^
{
NSLog(@"Changed 'someClassMethod'");
return 0;
}];
// Change instance method implementation
[MyClass changeInstanceMethod:@selector(someInstanceMethod)
implementation:^(MyClass *instance, NSObject *someArg)
{
NSLog(@"Called changed method of %@ with arg %@", instance, someArg);
}];
// Revert class method implementation
[MyClass revertClassMethod:@selector(someStaticMethod)];
// Revert instance method implementation
[MyClass revertInstanceMethod:@selector(someMethod)];
// Revert all methods
[MyClass revertMethods];
NSObject+OCFMethods
子spec包括了 Methods
和 Shared
子spec,作为依赖项。不要在 Podfile 中包含它们。
属性注入允许像处理 @synthesize
一样使用 @dynamic
属性。这是避免 "objective-c categories 中没有合成属性" 约束的最佳方式。注入基于 消息转发 和 关联对象。
安装
将 pod 'OCFuntime/Properties'
添加到 Podfile
示例
@interface SomeClass : NSObject
@property (nonatomic, strong) id objectStrongProperty;
@property (nonatomic, assign) NSInteger integerProperty;
@end
...
@implementation
@dynamic objectStrongProperty, integerProperty;
@end
...
#import "OCFuntime+Properties.h"
#import "SomeClass.h"
...
// Inject properties
OCFuntime *funtime = [[OCFuntime alloc] init];
[funtime injectClass:SomeClass.class property:@"objectStrongProperty"];
[funtime injectClass:SomeClass.class property:@"integerProperty"];
// Use properties
SomeClass *someInstance = [[SomeClass alloc] init];
someInstance.objectStrongProperty = [[NSObject alloc] init];
someInstance.integerProperty = 5;
// Remove injected property
[funtime removeClass:SomeClass.class property:@"objectStrongProperty"];
// Remove all class injected properties
[funtime removeClassProperties:SomeClass.class];
// Remove all injected properties
[funtime removeAllProperties];
注意
atomic
类型的注入视为 nonatomic
。这将在下一个版本中得到修复。OCFuntime
的 共享实例。forwardInvocation:
和methodSignatureForSelector:
。NSObject 分类
使用NSObject
分类在相应的类上运行属性注入。
将pod 'OCFuntime/NSObject+OCFProperties'
添加到Podfile
#import "NSObject+OCFProperties.h"
...
// Inject properties
[SomeClass injectProperty:@"objectStrongProperty"];
[SomeClass injectProperty:@"integerProperty"];
// Remove property
[SomeClass removeProperty:@"objectStrongProperty"];
// Remove all injected properties of class
[SomeClass removeProperties];
NSObject+OCFProperties
子规格包含Properties
和Shared
子规格作为依赖项。请勿将其添加到Podfile中。
共享实例
将pod 'OCFuntime/Shared'
添加到Podfile
#import "OCFuntime+Shared.h"
...
[OCFuntime.shared changeClass:MyClass.class
instanceMethod:@selector(someMethod)]
implementation:^
{
NSLog(@"Changed someMethod with shared instance")
}];
默认子规格
默认子规格pod 'OCFuntime'
包含所有子规格。使用@import "OCFuntimeHeader.h"
启用OCFuntime
的所有功能。
关注更新@okolodev