iOS hotfix lua語(yǔ)法

hotfix lua語(yǔ)法
  • 替換WSFRefundBaseCell
-- 替換WSFRefundBaseCell類里相關(guān)的方法
interface {"WSFRefundBaseCell"}

-- 替換實(shí)例方法
function initSubViews(self)
    print("begin------ WSFRefundBaseCell replace function initSubViews ------")
    -- self.super:initSubViews(self)
    -- 1庐船、ORIG:調(diào)用原有initSubViews方法
    self:ORIGinitSubViews()
    
    -- 創(chuàng)建UI + 布局
    local button = UIButton:buttonWithType(UIButtonTypeCustom)
    button:setTitle_forState("LuaButton", UIControlStateNormal)
    button:setTitleColor_forState(UIColor:blueColor(), UIControlStateNormal)
    button:setBackgroundColor(UIColor:lightGrayColor())
    self:containerView():addSubview(button)
    -- 2银酬、帶下劃線的方法:UNDERxLINE 代替 _
    toobjc(button):masUNDERxLINEmakeConstraints(
        toblock(
            function(make)
            make:top():equalTo()(self:containerView()):offset()(40)
            make:right():equalTo()(self:containerView()):inset()(20)
            -- make:top():masUNDERxLINEequalTo(self:containerView())
            -- make:right():masUNDERxLINEequalTo(self:containerView())
            end
            ,{"void", "MASConstraintMaker *"}
        )
    )
    
    -- 設(shè)置Label顏色
    local refundContentViewLeftLabel = self:refundContentView():refundMoneyLabel():leftLabel()
    refundContentViewLeftLabel:setTextColor(UIColor:blueColor())
    
    -- 原有UI 更新布局
    local rightLabel = self:refundContentView():titleView():rightLabel()
    rightLabel:setTextColor(UIColor:blueColor())
    local leftLabel = self:refundContentView():titleView():leftLabel()
    
    toobjc(rightLabel):masUNDERxLINEremakeConstraints(
        toblock(
            function(make)
            make:leading():equalTo()(leftLabel:masUNDERxLINEtrailing()):offset()(20)
            make:centerY():equalTo()(leftLabel)
            end
            ,{"void", "MASConstraintMaker *"}
        )
    )
    
    print("end------ WSFRefundBaseCell replace function initSubViews ------")
end


-- 替換實(shí)例方法
function setRefundModel(self, refundModel)
    self:ORIGsetRefundModel(refundModel)
    local applyPrice = string.format("Lua實(shí)際退款金額:%s",refundModel:realPrice())
    self:refundContentView():refundMoneyLabel():leftLabel():setText(applyPrice)
end
  • WSFRefundCellContentView類里新增屬性、方法
-- WSFRefundCellContentView類里新增屬性筐钟、方法
interface {"WSFRefundCellContentView", protocols = {"UITableViewDelegate", "UITableViewDataSource"}}

-- 新增 luaTableView 屬性
function luaTableView(self)
    if self._luaTableView == nil then 
        self._luaTableView = UITableView:init()
        self._luaTableView:setDelegate(self)
        self._luaTableView:setDataSource(self)
        self._luaTableView:setBackgroundColor(UIColor:lightGrayColor())
    end
    return self._luaTableView
end

function setLuaTableView(self, luaTableView)
    self._luaTableView = luaTableView
end

-- 新增方法(返回值和參數(shù)均為id類型) 
function initLuaSubViews(self)

    self.models = LuaTableViewModel:getLuaTableViewModels()

    local view = toobjc(self:luaTableView())
    local orderNumberLabel = self:orderNumberLabel()
    self:addSubview(view)
    -- view:setFrame(CGRect(10,10,300,200))
    print("打印orderNumberLabel = " .. tostring(orderNumberLabel))

    toobjc(view):masUNDERxLINEmakeConstraints(
        toblock(
            function(make)
                make:leading():equalTo()(orderNumberLabel:leftLabel():masUNDERxLINEtrailing())
                make:trailing():equalTo()(self)
                make:top():equalTo()(self)
                make:bottom():equalTo()(self)
            end
            ,{"void", "MASConstraintMaker *"}
        )
    )

    print("打印view = " .. tostring(view))
    return nil
end

function tableView_numberOfRowsInSection(self, table, section)
    local count = toobjc(self.models):count()
    return count
end

function tableView_cellForRowAtIndexPath(self, table, indexPath)
    local cell = table:dequeueReusableCellWithIdentifier("cellID")
    if cell == nil then 
        cell = UITableViewCell:initWithStyle_reuseIdentifier(UITableViewCellStyleDefault, "cellID")
    end
    local text = string.format("第%d個(gè)cell", indexPath:row())
    cell:textLabel():setText(text)
    local model = self.models[indexPath:row() + 1]
    cell:textLabel():setText(model:title())
    print("model = " .. tostring(model:title()))
    return cell
end
    
-- 替換initSubViews方法
function initSubViews(self)
    self:ORIGinitSubViews()
    self:initLuaSubViews(self)
end


-- 新增LuaTableViewModel類
interface {"LuaTableViewModel", NSObject}

-- 新增 title 屬性
function title(self)
    return self._title
end

function setTitle(self, title)
    self._title = title
end

-- 替換init方法
function init(self)
    self.super:init()
    return self
end

-- 新增類方法(返回值和參數(shù)均為id類型) 
function LuaTableViewModel:getLuaTableViewModels(self)
    -- 其中, start是起始值, limit是結(jié)束值, step是步進(jìn)(可省, 默認(rèn)是1).
    -- i是for循環(huán)的local變量, for循環(huán)之后i不存在.
    local arrays = {}
    for i = 10, 1, -1 do
        local model = LuaTableViewModel:init()
        local title = string.format("i = %s", tostring(i))
        model:setTitle(title)
        table.insert(arrays, model)
    end
    -- for i, v in pairs(arrays) do
    --  print("vtitle = " .. tostring(v:title()))
    -- end
    return arrays
end


hotfix lua語(yǔ)法
-- OC CGFloat width = [UIScreen mainScreen].bounds.size.width;
-- local width = UIScreen:mainScreen():bounds().width;

-- Lua對(duì)象轉(zhuǎn)換為OC對(duì)象:toobjc
-- local testString = "Hello lua!";
-- local bigFont = UIFont:boldSystemFontOfSize(30);
-- local size = toobjc(testString):sizeWithFont(bigFont);
-- puts(size);

-- Lua或者OC對(duì)象轉(zhuǎn)換為字符串:tostring
-- local num = 123;
-- local str = tostring(num);
-- print(tostring(xxx));

-- 調(diào)用類方法
-- [UT commitEvent:123 arg1:@"456" arg2:@"789"];
-- UT:commitEvent_arg1_arg2("123", "456", "789");

-- 替換類方法
-- function commitEvent_arg1_arg2(self, event, arg1, arg2);
-- self:ORIGcommitEvent_arg1_arg2(event, arg1, arg2);

-- 字典/數(shù)組
-- toobjc(self:muteDict()):objectForKey("k1")
-- toobjc(self:muteArray()):objectAtIndex(1)
-- toobjc(self:muteDict()):setObject_forKey("v11", "k1")
-- print(self:muteDict()["k1"])

 -- 新增方法(返回值和參數(shù)均為id類型)
 -- function myLostMethod(self)
 --         print("myLostMethod")
 --         return nil
 -- end
 -- function myLostMethod_other(self, a, b)
 --         print("myLostMethod_other");
 --         return nil
 --  end

-- 新增屬性
-- 方式1:直接使用lua自帶的新增屬性功能揩瞪,但是只能被lua使用
-- self.xx = "abcd"
-- print(self.xx)
-- 方式2:模擬OC的增加屬性功能,可以在OC訪問(wèn)
-- function xxx(self)
--    return self._xxx   
-- end
-- function setXxx(self, xxx)
--    self._xxx = xxx
-- end

-- 正確使用Lua字符串(Lua string不能直接調(diào)用NSString方法篓冲,否則會(huì)報(bào)nil錯(cuò)誤)
-- local x = string.len(str)
-- if not tempDeviceID == self:deviceID() then
-- end
-- string1 = "Lua" 
-- string2 = "Tutorial"
-- local s = string1 .. " abc " .. string2 --拼接
-- print(string.format("基本格式化 %s %s",string1,string2))

-- 將Lua字符串轉(zhuǎn)成 OC NSString 對(duì)象李破,再調(diào)用NSString的OC方法
-- local x = toobjc(str):length()
-- if not toobjc(tempDeviceID):isEqualToString(self:deviceID()) then
-- end
-- NSString *x = NSStringFromClass(“TestVC”)用local x = self:class():description() 代替

-- 日期格式化
-- date = 2; month = 1; year = 2014
-- print(string.format("日期格式化 %02d/%02d/%03d", date, month, year))

-- 數(shù)組(下標(biāo)從1開始!)
-- local array = {"a", "b", "c"}
-- table.insert(array, "d") --末尾添加
-- table.insert(array, 2, "e") --第二個(gè)位置插入
-- for i = 1, #array do
--  print(array[i])
-- end
-- for i, v in pairs(array) do --i為下標(biāo)
--  print(v)
-- end

-- 字典
-- local dict = {k1="v1", k2="v2"}
-- print(dict["k1"]) 
-- print(dict.k1)
-- 獲取到的對(duì)象已轉(zhuǎn)為table壹将,而table沒有這些方法嗤攻,想要使用OC的方法可以先通過(guò)toobjc
-- toobjc(self:muteDict()):objectForKey("k1")
-- toobjc(self:muteArray()):objectAtIndex(1)
-- toobjc(self:muteDict()):setObject_forKey("v11", "k1")
-- print(self:muteDict()["k1"])


hotfix lua語(yǔ)法參考文檔


lua語(yǔ)法常見報(bào)錯(cuò) attempt to index local 'make' (a nil value)
-- 在lua中調(diào)用方法一定要用冒號(hào)“:”,不然會(huì) attempt to index local 'make' (a nil value)
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末瞭恰,一起剝皮案震驚了整個(gè)濱河市屯曹,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌惊畏,老刑警劉巖恶耽,帶你破解...
    沈念sama閱讀 221,695評(píng)論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異颜启,居然都是意外死亡偷俭,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,569評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門缰盏,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)涌萤,“玉大人,你說(shuō)我怎么就攤上這事口猜「合” “怎么了?”我有些...
    開封第一講書人閱讀 168,130評(píng)論 0 360
  • 文/不壞的土叔 我叫張陵济炎,是天一觀的道長(zhǎng)川抡。 經(jīng)常有香客問(wèn)我,道長(zhǎng)须尚,這世上最難降的妖魔是什么崖堤? 我笑而不...
    開封第一講書人閱讀 59,648評(píng)論 1 297
  • 正文 為了忘掉前任侍咱,我火速辦了婚禮,結(jié)果婚禮上密幔,老公的妹妹穿的比我還像新娘楔脯。我一直安慰自己,他們只是感情好胯甩,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,655評(píng)論 6 397
  • 文/花漫 我一把揭開白布昧廷。 她就那樣靜靜地躺著,像睡著了一般蜡豹。 火紅的嫁衣襯著肌膚如雪麸粮。 梳的紋絲不亂的頭發(fā)上溉苛,一...
    開封第一講書人閱讀 52,268評(píng)論 1 309
  • 那天镜廉,我揣著相機(jī)與錄音,去河邊找鬼愚战。 笑死娇唯,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的寂玲。 我是一名探鬼主播塔插,決...
    沈念sama閱讀 40,835評(píng)論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼拓哟!你這毒婦竟也來(lái)了想许?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,740評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤断序,失蹤者是張志新(化名)和其女友劉穎流纹,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體违诗,經(jīng)...
    沈念sama閱讀 46,286評(píng)論 1 318
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡漱凝,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,375評(píng)論 3 340
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了诸迟。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片茸炒。...
    茶點(diǎn)故事閱讀 40,505評(píng)論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖阵苇,靈堂內(nèi)的尸體忽然破棺而出壁公,到底是詐尸還是另有隱情,我是刑警寧澤绅项,帶...
    沈念sama閱讀 36,185評(píng)論 5 350
  • 正文 年R本政府宣布紊册,位于F島的核電站,受9級(jí)特大地震影響趁怔,放射性物質(zhì)發(fā)生泄漏湿硝。R本人自食惡果不足惜薪前,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,873評(píng)論 3 333
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望关斜。 院中可真熱鬧示括,春花似錦、人聲如沸痢畜。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,357評(píng)論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)丁稀。三九已至吼拥,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間线衫,已是汗流浹背凿可。 一陣腳步聲響...
    開封第一講書人閱讀 33,466評(píng)論 1 272
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留授账,地道東北人枯跑。 一個(gè)月前我還...
    沈念sama閱讀 48,921評(píng)論 3 376
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像白热,于是被迫代替她去往敵國(guó)和親敛助。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,515評(píng)論 2 359

推薦閱讀更多精彩內(nèi)容

  • 用到的組件 1屋确、通過(guò)CocoaPods安裝 2纳击、第三方類庫(kù)安裝 3、第三方服務(wù) 友盟社會(huì)化分享組件 友盟用戶反饋 ...
    SunnyLeong閱讀 14,625評(píng)論 1 180
  • Lua介紹 Lua是什么(Lua程序設(shè)計(jì)語(yǔ)言是一個(gè)簡(jiǎn)潔攻臀、輕量焕数、可擴(kuò)展的腳本語(yǔ)言。Lua讀作/'lua/(嚕啊)茵烈,是...
    AZander閱讀 833評(píng)論 0 0
  • 項(xiàng)目需要集成熱修復(fù)百匆,解決線上緊急的缺陷,及時(shí)修復(fù)呜投,而無(wú)需另發(fā)版本到appstore加匈。主要考察了四種方案: Dyna...
    杭研融合通信iOS閱讀 2,370評(píng)論 4 11
  • 今天感恩節(jié)哎,感謝一直在我身邊的親朋好友仑荐。感恩相遇雕拼!感恩不離不棄。 中午開了第一次的黨會(huì)粘招,身份的轉(zhuǎn)變要...
    迷月閃星情閱讀 10,569評(píng)論 0 11
  • 彩排完啥寇,天已黑
    劉凱書法閱讀 4,223評(píng)論 1 3