NSDate转换为NSAttributedString发生Crash
OC代码
NSAttributedString *attrStr = [[NSAttributedString alloc] initWithData:[recordModel.match dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
JSPatch 代码
var attrStr = require('NSAttributedString').alloc().initWithData_options_documentAttributes_error(recordModel.match().dataUsingEncoding(10), { 'DocumentType': 'NSHTML' }, null, null)
描述 项目中有cell上有一个数据显示有问题,要修改数据显示,在一处要设置text的attributedText。初始页面没有问题,能够显示,但是一旦滚动tableView就会发生Crash。
崩溃信息

看堆栈跟webview有关,试试用performSelectorInOC调用这个方法 https://github.com/bang590/JSPatch/wiki/performSelectorInOC-%E4%BD%BF%E7%94%A8%E6%96%87%E6%A1%A3
还是会崩溃信息依然: 用了两种写法: 第一种:崩溃依然
self.performSelectorInOC('setKeyWordText', [recordModel.match()], function(ret) {
});
setKeyWordText 方法:
setKeyWordText:function(text) {
var attrStr = require('NSAttributedString').alloc().initWithData_options_documentAttributes_error(text.dataUsingEncoding(10), { 'DocumentType': 'NSHTML' }, null, null);
self.keyWordLabel().setAttributedText(attrStr);
return attrStr
}
第二种写法,没有成功执行perform 的block,不知道是哪里有问题
self.performSelectorInOC('setKeyWordText', [recordModel.match()], function(ret) {
self.keyWordLabel().setAttributedText(ret);
});
setKeyWordText 方法:
setKeyWordText:function(text) {
var attrStr = require('NSAttributedString').alloc().initWithData_options_documentAttributes_error(text.dataUsingEncoding(10), { 'DocumentType': 'NSHTML' }, null, null);
return attrStr
}
要return才行的,对照下文档
我的setKeyWordText方法以前OC没有,是在JS里面新添加的,应该是调用不了
更换了一个写法,但是遇到一个问题,就是attrStr在block中为空,是我哪里不对吗? readFromData_options_documentAttributes_error返回值一直是0
var attrStr = require('NSMutableAttributedString').alloc().init()
return attrStr.performSelectorInOC('readFromData_options_documentAttributes_error', [recordModel.match().dataUsingEncoding(10), { 'DocumentType': 'NSHTML' }, null, pError], function(ret) {
console.log(attrStr);
});
var attrStr = require('NSMutableAttributedString').alloc().init()
return attrStr.performSelectorInOC('readFromData_options_documentAttributes_error', [recordModel.match().dataUsingEncoding(10), { 'DocumentType': 'NSHTML' }, null, pError], function(ret) {
attrStr = ret;
console.log(attrStr);
});
"readFromData_options_documentAttributes_error" 这个方法返回的是bool值
喔跟前面提问的不一样了 搞混了
recordModel.match().dataUsingEncoding(10)这个确定有值?
确定是有值的
我也遇到了相同的问题,实用JSPatch 之后 会导致原生代码 中的 NSAttributedString initData,crash
@Cooriyou 我最近也在查这个问题,我的app是支持IOS8.3以后的,NSAttributedString initData,crash,有些人会遇到,但是有些人又没有,你怎么发现是和JSPatch有关的?
@jezzmemo 一步步排除,一步步缩小范围,恢复Native code 就是没问题,一上脚本就可见的崩溃。
@Cooriyou 我的好像不是这个问题,你的问题是你脚本中用到了NSAttributedString? 我的直接native code就会偶尔报crash
@jezzmemo me too ,脚本无 NSAttributedString
我们也遇到了。使用了Patch对table view做reload, 导致 native code中的NSAttributedString crash. 解析的是NSHTMLTextDocumentType的data