在一次不細(xì)心的操作過程中,將一個(gè)bool對(duì)象改為nsstring時(shí),忘記將修飾assign改為copy了
@property (assign, nonatomic) NSString *selectedWord;
這是第一塊代碼,第二塊代碼如下
- (void)translatorWebView:(TRTranslatorWebView *)translatorWebView didSelectText:(NSString *)selectedText {
// 此處對(duì)selectedText做了去除首尾空格處理,這是我第二塊代碼問題的關(guān)鍵
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) {
// 網(wǎng)絡(luò)錯(cuò)誤
NSDictionary *commonConfig = [TRCommon configData:kTextTranslatorConfig];
TRWord *wordObject = [[TRWord alloc] init];
wordObject.name = self.selectedWord; // 此處selectedWord對(duì)象變成野指針了
wordObject.definitions = [BKCommon currentNetworkStatus] == BKNotReachable ? commonConfig[@"briefViewNotReachable"]:commonConfig[@"briefViewFaileText"];
[[NSNotificationCenter defaultCenter] postNotificationName:TRDataManagerDidTranslateTextNotification object:self userInfo:@{@"word" : wordObject}];
return;
}else{
[dataManager translateText:dictionary];
}
}
}
crash場(chǎng)景:以上代碼的情況下,出現(xiàn)在選中的換行文本處,應(yīng)該是換行的地方有什么特例情況,觸發(fā)assign修飾的string對(duì)象變成野指針
解決:將nsstring改為正確的修飾copy(不改回copy還用assign的情況下,不用去空格代碼也不會(huì)crash,當(dāng)然寫法也不規(guī)范,我是對(duì)這個(gè)特殊觸發(fā)場(chǎng)景比較疑惑)