關(guān)于:
JSPatch -- 熱修復(fù)BUG神器, 大公司會(huì)用它來(lái)做模塊更新, 而且配合React native會(huì)更好. 關(guān)于它的一些介紹就不廢話(huà)了, 直接上說(shuō)做 hot fix js斷點(diǎn)調(diào)試 中遇到的問(wèn)題
js斷點(diǎn)調(diào)試步驟
1 推薦代碼優(yōu)先使用工具轉(zhuǎn)換會(huì)省很多時(shí)間 : JSPatchConvertor
2 將OC代碼轉(zhuǎn)換后, 保存成main.js文件, 拖到項(xiàng)目中去
3 在app delegate里面寫(xiě)上
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// 用于發(fā)布前測(cè)試腳本。先把腳本放入項(xiàng)目中,調(diào)用后,會(huì)在當(dāng)前項(xiàng)目的 bundle 里尋找 main.js 文件執(zhí)行
[JSPatch testScriptInBundle];
return YES;
}
4 轉(zhuǎn)換后啟動(dòng) js斷點(diǎn)調(diào)試 : JS-斷點(diǎn)調(diào)試
-
5 在真機(jī)上運(yùn)行項(xiàng)目
Safari會(huì)彈出Safari Web檢查器
Snip20170120_1.png- 如果沒(méi)有彈出
Safari Web檢查器, 直接點(diǎn)擊JSContext
- 如果沒(méi)有彈出

相對(duì)于js不熟悉的, 仔細(xì)看文檔 : JSPatch用法
遇到的問(wèn)題
- 1 關(guān)于API
defineClass
Snip20170120_3.png
-
注意點(diǎn) :
如果沒(méi)有新增property,字符串?dāng)?shù)組,這一項(xiàng)可省略, 有的話(huà)就加進(jìn)去數(shù)組, 在這里假設(shè)有屬性property
-
如果既有
類(lèi)方法, 又有實(shí)例方法, 分別用一個(gè)括號(hào)擴(kuò)住方法用逗號(hào)隔開(kāi),實(shí)例方法在前,類(lèi)方法在后
// JS
defineClass("JPTableViewController", ['data', 'totalCount'], {
//實(shí)例方法
}, {
//類(lèi)方法
shareInstance: function() {
...
},
})
+ **如果有 `類(lèi)` 方法, 沒(méi)有 `實(shí)例方法` , 前面裝`實(shí)例方法`{} 不能省略**
```
// JS
defineClass("JPTableViewController", ['data', 'totalCount'], {}, {
//類(lèi)方法
shareInstance: function() {
...
},
})
+ **如果沒(méi)有 `類(lèi)` 方法, 有 `實(shí)例方法` , 后面裝`類(lèi)`方法的{} 不能省略**
```
// JS
defineClass("JPTableViewController", ['data', 'totalCount'], {
//實(shí)例方法
}, {})
- **舉個(gè)栗子** : 真機(jī)運(yùn)行, 點(diǎn)擊單步調(diào)試, 會(huì)定位了崩潰的錯(cuò)誤地點(diǎn)
鼠標(biāo)放上去定位具體錯(cuò)誤地點(diǎn)
**解決 : 查詢(xún)文檔是locate后面不應(yīng)該有()**

- **去掉上面 `locate` 后面的這個(gè)括號(hào)** : 繼續(xù)運(yùn)行程序, 點(diǎn)擊單步調(diào)試, 異常結(jié)果如下 : 找不到這個(gè)宏

> 查詢(xún)文檔得知, `Objective-C 里的常量/枚舉不能直接在 JS 上使用
+ **解決過(guò)程** : 可以直接在 JS 上用 `具體值`代替 `NSNotFound ` 在OC中按住command點(diǎn)擊 查看是 `static const NSInteger NSNotFound = NSIntegerMax;`
繼續(xù)點(diǎn)擊查詢(xún)發(fā)現(xiàn)`#define NSIntegerMax LONG_MAX` 本身是一個(gè)宏, 無(wú)法得知具體數(shù)值, 只能打印`NSLog(@"宏: %ld", (long)NSNotFound);` 得到結(jié)果 `9223372036854775807`
+ **解決** 用上面 `log` 的數(shù)值取代`main.js`里面的`NSNotFound`,運(yùn)行程序, 不會(huì)報(bào)錯(cuò)了.

