出現問題是在 SimulatorApp.mm 文件裏
- (void)handleNotification:(NSNotification *)note
{
// NSLog(@"Received notification: %@", note);
[_pipeReadHandle readInBackgroundAndNotify] ;
NSData *data = [[note userInfo] objectForKey:NSFileHandleNotificationDataItem];
NSString *str = [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease];
//show log to console
[_consoleController trace:str];
if(_fileHandle!=nil){
[_fileHandle writeData:[str dataUsingEncoding:NSUTF8StringEncoding]];
}
}
NSString *str = [[[NSStringalloc]initWithData:dataencoding:NSUTF8StringEncoding]autorelease];
轉換成utf8時 str 爲nil, 導致
[_consoleControllertrace:str];
方法
[[NSAttributedStringalloc]initWithString:msgattributes:attrsDictionary];
報出了 msg 爲 nil 然後模擬器卡死。
問:爲什麼str 會爲空呢?
答:data如果包含非utf8字符就會返回空,雖然data 可能有數據。
問:爲什麼data會有非utf8字符呢?
答:可能是由於中文原因,你的文件如果是gbk的編碼格式,如果打印沒進行轉碼就是導致這個問題。因爲我是用lua寫的,一個文件不知道什麼情況變成了gbk編碼。
就一直包這個錯。
解決方法:
找到不是utf8編碼的文件修改成 utf8。或者打印執行一次轉碼。