json解析,同樣的請求,有一個請求,無反應。糾結了幾天,終於解決了。
error = Error Domain=NSCocoaErrorDomain Code=3840 "Unescaped control character around character 168." UserInfo={NSDebugDescription=Unescaped control character around character 168.}
報錯信息如上:
這個原因,是因為服務器返回的字符串里面有換行符,所以我們要在接收到的數據里面,將換行符替換掉,然后再轉模型。
但是,AFN解析,並沒有提供原有的數據給我們(我沒找),就直接去了error 的接口。
然后用session的dataTask,用字符串轉它返回的data,則能夠把數據讀取出來,打印,果然有換行符。於是,改寫了原來的方法,用session去代替。
NSString * url = infoCategoryM.listUrl;
NSURL * URL = [NSURL URLWithString:url];
NSURLRequest * request = [NSURLRequest requestWithURL:URL];
NSURLSession * session = [NSURLSession sharedSession];
NSURLSessionDataTask * dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
NSString * str = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
NSString * str2 = [str stringByReplacingOccurrencesOfString:@"\t" withString:@""];
str2 = [str2 stringByReplacingOccurrencesOfString:@"\n" withString:@""];
str2 = [str2 stringByReplacingOccurrencesOfString:@"\r" withString:@""];
ContentListArrM * contentListArrM = [[ContentListArrM alloc]initWithString:str2 error:nil];
[self.contentListArr removeAllObjects];
for (int i = 0; i < contentListArrM.contentList.count ; i++) {
ContentListM * m = [contentListArrM.contentList objectAtIndex:i];
if (![XSDKResourceUtil xsdkstringIsnilOrEmpty:m.title]) {
[self.contentListArr addObject:m];
}
}
dispatch_async(dispatch_get_main_queue(), ^{
[self.infoTableView reloadData];
self.selectedButton.selected = NO;
selectedButton.selected = YES;
self.selectedButton = selectedButton;
NewsTableViewCell * cell = [self.infoTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:0]];
cell.selected = YES;
});
}];
[dataTask resume];
里面,替換掉了換行符等其他字符,然后再轉模型。
This application is modifying the autolayout engine from a background thread, which can lead to engine corruption and weird crashes. This will cause an exception in a future release.
這個錯誤,是告訴我們,要在主線程里面更新UI。
