forked from probablycorey/wax
-
Notifications
You must be signed in to change notification settings - Fork 280
UseInSwift CN
junzhan edited this page Dec 18, 2015
·
1 revision
- 安装Wax
- 创建Bridging-Header, 在里面
#import <wax/wax.h>
- 如果在Swift代码中初始化Wax则使用wax_startWithNil()
- SwiftExample
- waxClass需要加上Swift的module名称,如:
waxClass{"SwiftExample.TestSwiftVC"}
function viewDidLoad(self)
print("lua viewDidLoad");
self:ORIGviewDidLoad();
end
- 调类方法需要使用objc_getClass(先调
luaSetWaxConfig({openBindOCFunction='true'})绑定OC内置函数
),如:
objc_getClass("SwiftExample.TestSwiftVC"):testClassReturnVoidWithaId(self:view())
- 创建Swift实例
local vc = objc_getClass("SwiftExample.TestBSwiftVC"):initWithNibName_bundle("TestBSwiftVC", nil);
local a = objc_getClass("SwiftExample.TestASwiftClass"):init()
纯Swift(没有继承自NSObject)本身不具有runtime特性,函数调用在编译时就已经确定,所以纯Swift的方法没法运行时hook. 但是Swift为了兼容Objective-c,所以增加了@objc和@dynamic的修饰让方法可以强制使用运行时调用. 因此需要做如下步骤:
- Swift类继承NSObject
- Swift方法、属性前加dynamic修饰 (继承自Objective-c的方法可以不加, 但一些Swift特有的类型如Character、Tuple无法添加dynamic修饰,会编译错误)
例如
class TestASwiftClass :NSObject{
dynamic var aBool:Bool = true;
dynamic var aInt:UInt = 0;
dynamic var aDouble:Double = 1234.567;
dynamic func testReturnVoidWithaId(aId:UIView){
print("F:\(__FUNCTION__) L:\(__LINE__)");
self.performSelector(Selector("testNoExistMethod"));
}
}
class TestSwiftVC: UIViewController, UITableViewDelegate, UITableViewDataSource {
dynamic var aString:String = "abc";
dynamic var aObject:AnyObject! = nil;
override func viewDidLoad() {
}
override func viewDidAppear(animated: Bool) {
}
dynamic func callTestASwiftClass(){
}
dynamic func setupTableView(){
}
}
PS:需要给Swift方法加上dynamic比较恶心,如果有更好的方法,请告知我,Thanks!