OCEval
一个迷你 Objective-C JIT 解释器,动态运行您的代码,就像 eval()
。
特性
- 动态运行 Objective-C 代码。
- 支持 iOS 和 OS X。
- 用 Objective-C 编写。
- 驱动于单元测试。
- 支持部分低级 API,如 block 和 C 函数。
用法
动态调用 Objective-C 方法
//Example 1
NSString *inputStr = @"return 1 + 3 <= 4 && [NSString string] != nil;";
NSNumber *result = [OCEval eval:inputStr];
NSAssert([result boolValue] == YES, nil);
//Example 2
NSString *inputStr = @"{NSArray *content = @[@6,@7,@8,@9,@1,@2,@3,@4];\
NSComparisonResult (^comparison)(id obj1, id obj2) = ^NSComparisonResult(id _Nonnull obj1, id _Nonnull obj2) {\
return [obj1 doubleValue] > [obj2 doubleValue];\
};\
content = [content sortedArrayUsingComparator:comparison];\
return content;\
}";
NSArray *result = [OCEval eval:inputStr];
NSAssert([result[6] intValue] == 8, nil);
动态替换方法实现
//new implementation code,just call originalInvocation
NSString *viewDidLoad2 = @"{\
[originalInvocation invoke];\
";
[OCEval hookClass:@"ViewController"
selector:@"viewDidLoad"
argNames:@[]
isClass:NO
implementation:viewDidLoad2];
甚至制作一个动态应用程序
理论上,我们可以编写一个全部由 Objective-C 编写的应用程序,并通过网络交付。您可以查看 iOS 演示以了解详细信息。
安装
Cocoapods
pod 'OCEval'
待办事项列表
已支持的语法
- if..else..,do..while..
- for..in,for
- @() @[] @{}
- array[0] or dic[@""]
- block
- 调用 C 外部函数
- mac 应用程序
- i++,++i,i+=1
- if(a){}
- C struct: frame.origin.x 查看 YYModel。
尚不支持
- 调用 C 内联函数
- 宏如 #define xx 或 typedef:使用原始值。
- ((YES)&&(NO)) : 使用
(YES && NO)
代替 - [stringformat:@"%d",aInt] : 使用
[stringformatweakSelf [~, gems]]" instead
- ?: : 使用
if else
代替 - _propertyName : 使用
self.propertyName
代替 - 如果 (!a),请使用
if(a == nil)
代替
警告
包含此框架的应用程序提交到AppStore可能会被拒绝。
依赖
- libffi
- 方面