OCFuntime 0.3.1-beta1

OCFuntime 0.3.1-beta1

测试测试过的
语言语言 Obj-CObjective C
许可证 MIT
发布上次发布2016 年 6 月

Alexey Belkevich 维护。



OCFuntime 0.3.1-beta1

关于

OCFuntime 是一个针对 Objective-C 运行时的工具包。

特点:

  • 更改实例或类方法实现并恢复
  • 向任何类注入任何类型的属性
  • 模块化结构:每个任务提取为 Cocoapods 子spec

更改方法实现

方法实现更改允许在相应的方法调用上运行代码块。不要与 方法交换 混淆。

实例方法实现代码块应遵守签名

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 实例释放后,所有更改的方法都将恢复到默认实现。为了避免这种情况,请使用 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包括了 MethodsShared 子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 实例释放后,所有注入的属性将被删除。为了避免这种情况,请使用 OCFuntime共享实例
  • 如果属性没有在类接口中定义,将引发异常。
  • 如果属性已经合成或已注入,将引发异常。
  • 由于方法交换(Swizzling),属性注入不会破坏方法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子规格包含PropertiesShared子规格作为依赖项。请勿将其添加到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