招牌英雄鎮(zhèn)樓
這里主要用到的是某云爸爸旗下的熱修復(fù)技術(shù)。代碼是基于Lua寫的谣膳。(轉(zhuǎn)載請注明出處)
初期在接觸Lua
的Hotfix時,沒有學(xué)習(xí)過Lua
的語法。在轉(zhuǎn)化成OC
原生代碼時酌呆,費了好大勁。各種地方去搜索都找不到如何轉(zhuǎn)換搔耕。手里只有一點點的文檔隙袁。沒辦法,只能各種嘗試弃榨。
<主要為了記錄一下lua代碼轉(zhuǎn)換成OC代碼,方便大家學(xué)習(xí)參考>
替換原有的方法
1 定位到具體的類文件
interface{"XXXController"}
需要注意的是 interface是不需要end結(jié)尾的
2 定位到具體的方法
function 方法名稱(self)
function 是需要end結(jié)尾菩收。 意思就是在這個方法結(jié)束后,要在最后加一個end表示結(jié)束鲸睛。
需要注意這里的self 娜饵,不能漏掉,不然在方法內(nèi)部腊凶,拿不到這個類是屬性值划咐。換句話說就是在方法內(nèi)部所有的self都是空的。 會造成crash钧萍。
多個參數(shù)的方法使用下劃線拼接褐缠,括號里依次傳遞參數(shù)。這里拿tableview的一個必須實現(xiàn)的dataSource方法為例子
在OC的代碼里风瘦,是這樣的
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
轉(zhuǎn)換成Lua的代碼如下
function tableView_ cellForRowAtIndexPath(self, tableView, indexPath)
3 對象的創(chuàng)建
在創(chuàng)建對象時队魏,不需要alloc, 直接init万搔,加上冒號括號胡桨,例如:
local view = UIView:init()
獲取OC屬性使用方法,不能使用 點語法 瞬雹,例如:
view:backgroundColor()
設(shè)置OC屬性使用set方法昧谊,不能直接 =
self:label():setText('XXX')
4 簡單的實戰(zhàn)
在需要替換的方法中,有部分是不需要修改的酗捌,例如
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.title = @"修改前";
[self loadData];
}
我只是想將 @"修改前" 修改為 @"修改后"呢诬。 而其他的不變。 轉(zhuǎn)化為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能定位的最小單位是方法哪廓。不能定位到某一行的代碼狗唉。如果這個方法里面的代碼較多,就很無奈了涡真,要挨著轉(zhuǎn)為Lua分俯。 其實并不需要這么麻煩肾筐,我們可以這樣寫
function viewDidLoad(self)
self.super:viewDidLoad(self)
if XXX == '判斷1'
then
self:navigationItem():setTitle('修改后');
else
self:ORIGviewDidLoad() --不要傳self
end
end
創(chuàng)建 / 新增一個UIViewController的類
interface{"XXXController", UIViewController}
function init(self)
self.super:initWithNibName_bundle("MyControllerView.xib", nil)
return self
end
function viewDidLoad(self)
end
新增一個類,并聲明協(xié)議澳迫,例如
interface{"XXXClass", NSObject, protocols = {"UITableViewDelegate", "UITableViewDataSource”}}
實戰(zhàn)中局齿,遇到的一些坑 (持續(xù)更新)
Lua中返回的字符串均為Lua string, 不能直接調(diào)用NSString的方法, 否則會報nil value錯誤
if stringOne:isEqualToString('stringTwo') then
end
正確的做法如下
if stringOne == 'stringTwo' then
end
或者可以將string轉(zhuǎn)化為OC的對象來使用OC的方法
if toobjc(stringOne):isEqualToString('stringTwo') then
end
NSArray / NSDictonary在Lua中的使用
NSArray橄登、NSDictionary在Lua中并不能像普通對象一樣使用抓歼,因為他們會被wax轉(zhuǎn)換成table,所以取而代之的是在Lua 中使用table來實現(xiàn)這兩者的功能拢锹。
在Lua腳本里不能對Array谣妻、Dictionary進(jìn)行初始化,如下用法都會造成crash卒稳。
NSMutableArray:init()
NSMutableArray:arrayWithObjects("1","2")
NSMutableDictionary:init()
NSMutableDictionary:dictionaryWithObjectsAndKeys("v1","k1")
初始化:
local array = {"a", "b", "c"}
下標(biāo)訪問(注意Lua的數(shù)組下標(biāo)都是從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為下標(biāo) print(v)
end
self:muteArray():objectAtIndex(1) 會崩潰
toobjc(self:muteArray()):objectAtIndex(1) 正確
熱修復(fù)需要注意:
對于已經(jīng)通過熱修復(fù)解決的問題蹋半,下個版本要替換為新的native代碼;
熱修復(fù)對應(yīng)的補(bǔ)丁要充分考慮相關(guān)業(yè)務(wù)影響充坑,測試充分减江,避免熱修復(fù)帶來新問題;
------分割線------ ------分割線------ ------分割線------ ------分割線------
字符串拼接 local url =" string" .. "stringOther"
調(diào)用類的屬性 例如數(shù)組 self:dataSource()
獲取數(shù)組長度 local count = table.getn(self:dataSource());
設(shè)置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)
根據(jù)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('當(dāng)前時間為空')
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