iOS由assign和去除空格whitespaceAndNewlineCharacterSet引發(fā)的崩潰

在一次不細(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)景比較疑惑)

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容