在一次不细心的操作过程中,将一个bool对象改为nsstring时,忘记将修饰assign改为copy了
@property (assign, nonatomic) NSString *selectedWord;
这是第一块代码,第二块代码如下
- (void)translatorWebView:(TRTranslatorWebView *)translatorWebView didSelectText:(NSString *)selectedText {
// 此处对selectedText做了去除首尾空格处理,这是我第二块代码问题的关键
self.selectedWord = [selectedText stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
if (![self.selectedWord isEqualToString:@""]) {
[self.delegate textTranslator:self webView:translatorWebView withText:self.selectedWord];
}else{
[textConciseView hideBriefViewAnimations];
}
}
- (void)didGotTranslation:(id)dictionary{
[self.delegate textTranslator:self withEventDic:@{@"eventName":@"TranslationBar"}];
if (dictionary != nil ) {
if (dictionary[@"error"] != nil) {
// 网络错误
NSDictionary *commonConfig = [TRCommon configData:kTextTranslatorConfig];
TRWord *wordObject = [[TRWord alloc] init];
wordObject.name = self.selectedWord; // 此处selectedWord对象变成野指针了
wordObject.definitions = [BKCommon currentNetworkStatus] == BKNotReachable ? commonConfig[@"briefViewNotReachable"]:commonConfig[@"briefViewFaileText"];
[[NSNotificationCenter defaultCenter] postNotificationName:TRDataManagerDidTranslateTextNotification object:self userInfo:@{@"word" : wordObject}];
return;
}else{
[dataManager translateText:dictionary];
}
}
}
crash场景:以上代码的情况下,出现在选中的换行文本处,应该是换行的地方有什么特例情况,触发assign修饰的string对象变成野指针
解决:将nsstring改为正确的修饰copy(不改回copy还用assign的情况下,不用去空格代码也不会crash,当然写法也不规范,我是对这个特殊触发场景比较疑惑)
网友评论