最近开始用JSPatch修复线上的bug,但是使用过程中(其实主要是指OC代码转换成JS代码的过程中)遇到了一些问题,特此记录下来作为自己的备忘录,同时也希望能帮助一些人。
转换问题列表:
序号 | 问题 | 处理方法(JS代码) | 原因 |
---|---|---|---|
1 | NSMakeRange(16,1); | {location: 16, length: 1} | NSMakeRange是构造结构体的内联方法,JSPatch不支持,推荐使用固定的结构体定义方法 |
2 | [button setTitle:@”确定” forState:UIControlStateNormal]; | button().setTitle_forState(“确定”, 0); | UIControlStateNormal是系统常量,JSPatch不支持,推荐直接使用常量的值 |
3 | __weak typeof(self) weakSelf = self; | var weakSelf = __weak(self); | typeof()函数JSPatch不支持,JSPatchConvertor会报错Syntax Error,推荐直接使用self的类型;另外__weak修饰符JSPatchConvertor无法转换,需要在转换后收到添加; |
4 | NSString *webURL = [SWLUtil redirectURL:SWL_Test_URL]; | var webURL = SWLUtil.redirectURL(“http://www.test.com“); | SWL_Test_URL为字符串宏,而JSPatch不支持宏定义,所以需要直接使用字符串值; |
5 | NSString *testURL = [baseURL stringByAppendingString:@”/api/test”]; | var testURL = “http://www.test.com/api/test“; | baseURL的值为”http://www.test.com“字符串,而JSPatch运行时,无法转自动转换成字符串对象,所以简单的办法是直接放拼接好的字符串;如果实在无法处理,就要用NSString的类方法去显式地创建字符串对象 |
6 | NSArray *array = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookiesForURL:[NSURL URLWithString:webURL]]; for (NSHTTPCookie *cookie in array){ [[NSHTTPCookieStorage sharedHTTPCookieStorage] deleteCookie:cookie]; } | for (var index = 0; index < array.count(); index++) { var cookie = array.objectAtIndex(index); cookieStorage.deleteCookie(cookie); } | 使用forin语法时,自动转换出来的JS代码无法识别出cookie对象,改成for语法后运行正确。 |
7 | [SWLTest testWithBlock:^{ [self output]; }]; | var jsSelf = self;SWLTest.testWithBlock(block(function(){jsSelf.output();})); | 即便是不考虑block中直接使用导致的循环引用问题,JS代码中也不能直接使用self; |