iOS 热修复 基于Lua语法 (hotfix)

2,657 阅读4分钟

招牌英雄镇楼

DLW.jpeg

这里主要用到的是某云爸爸旗下的热修复技术。代码是基于Lua写的。(转载请注明出处)

初期在接触Lua的Hotfix时,没有学习过Lua的语法。在转化成OC原生代码时,费了好大劲。各种地方去搜索都找不到如何转换。手里只有一点点的文档。没办法,只能各种尝试。

<主要为了记录一下lua代码转换成OC代码,方便大家学习参考>

替换原有的方法

1 定位到具体的类文件

interface{"XXXController"}

需要注意的是 interface是不需要end结尾的

2 定位到具体的方法

function 方法名称(self) function 是需要end结尾。 意思就是在这个方法结束后,要在最后加一个end表示结束。

需要注意这里的self ,不能漏掉,不然在方法内部,拿不到这个类是属性值。换句话说就是在方法内部所有的self都是空的。 会造成crash。

多个参数的方法使用下划线拼接,括号里依次传递参数。这里拿tableview的一个必须实现的dataSource方法为例子 在OC的代码里,是这样的

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

转换成Lua的代码如下

function tableView_ cellForRowAtIndexPath(self, tableView, indexPath)

3 对象的创建

在创建对象时,不需要alloc, 直接init,加上冒号括号,例如:

local view = UIView:init()

获取OC属性使用方法,不能使用 点语法 ,例如:

view:backgroundColor()

设置OC属性使用set方法,不能直接 =

self:label():setText('XXX')

4 简单的实战

在需要替换的方法中,有部分是不需要修改的,例如

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"修改前";
    [self loadData];
}

我只是想将 @"修改前" 修改为 @"修改后"。 而其他的不变。 转化为Lua如下

function viewDidLoad(self)
self.super:viewDidLoad(self) -- super很特殊,需要用 . 操作
self:navigationItem():setTitle('修改后');
self:loadData(self)
end

还要另一种比较头疼的场景

- (void)viewDidLoad {
    [super viewDidLoad];
    if ([XXX isEqualToString:@"判断1"]) {
         self.navigationItem.title = @"修改前";
    }else{
        //一大堆没有错的无辜代码。 不需要修改
    }
  }

按照我之前的理解,Lua能定位的最小单位是方法。不能定位到某一行的代码。如果这个方法里面的代码较多,就很无奈了,要挨着转为Lua。 其实并不需要这么麻烦,我们可以这样写

function viewDidLoad(self)
self.super:viewDidLoad(self) 
if XXX == '判断1'
	then 	
        self:navigationItem():setTitle('修改后');
else
        self:ORIGviewDidLoad()  --不要传self
end
end

创建 / 新增一个UIViewController的类

interface{"XXXController", UIViewController}
function init(self)
self.super:initWithNibName_bundle("MyControllerView.xib", nil)
return self
end

function viewDidLoad(self)
end

新增一个类,并声明协议,例如

interface{"XXXClass", NSObject, protocols = {"UITableViewDelegate", "UITableViewDataSource”}}

实战中,遇到的一些坑 (持续更新)

Lua中返回的字符串均为Lua string, 不能直接调用NSString的方法, 否则会报nil value错误

if  stringOne:isEqualToString('stringTwo') then
end

正确的做法如下

if  stringOne == 'stringTwo' then
end

或者可以将string转化为OC的对象来使用OC的方法

if toobjc(stringOne):isEqualToString('stringTwo') then
end

NSArray / NSDictonary在Lua中的使用 NSArray、NSDictionary在Lua中并不能像普通对象一样使用,因为他们会被wax转换成table,所以取而代之的是在Lua 中使用table来实现这两者的功能。 在Lua脚本里不能对Array、Dictionary进行初始化,如下用法都会造成crash。

NSMutableArray:init() 
NSMutableArray:arrayWithObjects("1","2") 
NSMutableDictionary:init() 
NSMutableDictionary:dictionaryWithObjectsAndKeys("v1","k1")

初始化:

local array = {"a", "b", "c"}

下标访问(注意Lua的数组下标都是从1开始!): 添加元素:

table.insert(array, "d")--末尾添加 
table.insert(array, 2, "e")--插入到第二个位置
删除元素:
table.remove(array)--删除末尾元素
table.remove(array, 2)--删除第二个元素
遍历:
 for i = 1, #array do
    print(array[i])
      end
for i, v in pairs(array) do --i为下标 print(v)
end

self:muteArray():objectAtIndex(1) 会崩溃 toobjc(self:muteArray()):objectAtIndex(1) 正确

------分割线------ ------分割线------ ------分割线------ ------分割线------

字符串拼接 local url =" string" .. "stringOther" 调用类的属性 例如数组 self:dataSource() 获取数组长度 local count = table.getn(self:dataSource()); 设置textField键盘类型 self:XTextField():setKeyboardType(UIKeyboardTypeNumbersAndPunctuation); BOOL 初始化 local BOOOOOL = NSNumber:numberWithBool(true) local dict = {keyOne='value',keyTwo= BOOOOOL} 给imageView赋值图片 self:imageView():setImage(UIImage:imageNamed("XXX")) 获取字符串长度 local strLength = string.len(string) 根据indexSection 取值 local str = toobjc(self:rankArray()):objectAtIndex(indexPath:section())

判断一个值不为nil

if XXX ~= nil
	then 	
        end
else
end

masonry 布局方法

local imageV = UIImageView:init()
self:view():addSubview(imageV)
toobjc(imageV):masUNDERxLINEmakeConstraints(
    toblock(
        function(make)
            make:top():equalTo()(toobjc(self: view()):masUNDERxLINEtop())
            make:bottom():equalTo()(toobjc(self: view()):masUNDERxLINEbottom())
            make:left():equalTo()(toobjc(self: view()):masUNDERxLINEleft())
            make:right():equalTo()(toobjc(self: view()):masUNDERxLINEright())
        end
        ,{'id', 'id'}
    )
)
gsSetConfig({bind_ocf=true})
interface{"DZJMedicineDetailController"}

function getTimeCountDonwByStartDate(self,curDate)
if curDate == nil
then DZJToast:toast('当前时间为空')
return
end

local  startDate = NSDate:date()

--新增属性
self.timeInterval = toobjc(curDate):timeIntervalSinceDate(startDate);

local countDownTimer  = NSTimer:scheduledTimerWithTimeInterval_block_repeats(1.0,
	toblock(
		function(timer)

if self.timeInterval <= 0
	then
		toobjc(sender):invalidate();
		setSender(nil)
		dispatch_async(dispatch_get_main_queue(),
			toblock(
				function( )
						self:loadCustomDataByFirstLoad(NO);
				end)
			);
else
	local days = math.floor((self.timeInterval/(3600*24)))
	local hours = math.floor(((self.timeInterval-days*24*3600)/3600));
	local minute = math.floor((self.timeInterval-days*24*3600-hours*3600)/60);
	local second = math.floor((self.timeInterval-days*24*3600-hours*3600-minute*60));
	dispatch_async(dispatch_get_main_queue(),
		toblock(
		function( )
			print('主线程赋值')
			local tipLabel = toobjc(self:onlineTipView():subviews()):firstObject();
			print(tipLabel)
			local patientInfoText =  string.format("直播倒计时: %02d:%02d:%02d",(days*24+hours),minute,second)
			tipLabel:setText(patientInfoText)
		end)
	);
	self.timeInterval =  self.timeInterval - 1
end

		end),true);

end
function prepareForReuse(self)
	self.super:prepareForReuse(self);
	self:urgentImg():setImage(nil)
	self:imageBgView():removeAllSubviews();
end
if self:gotoMoreAction() ~= nil
     then						    
    gsCallBlockWithParamsType(self:gotoMoreAction(), {"void","NSInteger"}, 7)
end