在UITextview中显示响应时出错 [英] Error showing response in UITextview

查看:41
本文介绍了在UITextview中显示响应时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有关此问题答案

我正在尝试在textview中显示输出字符串,但是将其杀死.请检查

  NSLog(@"viewcontroller response =%@",responseString);txt_output.text = [NSString stringWithFormat:@%@",strResponse]; 

txt_output在我的ViewcontrollerB中,我正在从ViewcontrollerA返回

  NSString * returnstring = [NSString stringWithFormat:@"request =%@ responstring =%@ error =%@",request,responseString,error];completeHandler(responseString); 

在日志上产生共振,但是在textview上显示时却消失了

  viewcontroller响应= request =< NSMutableURLRequest:0x786b1b30>{URL:myhosturl} responstring =错误=错误域= NSURLErrorDomain代码= -1001操作无法完成.(NSURLErrorDomain错误-1001.)" UserInfo = 0x786bb400 {NSErrorFailingURLKey = http://myhosturl;NSErrorFailingURLStringKey = myhosturl,NSUnderlyingError = 0x7a1790d0操作无法完成.(kCFErrorDomainCFNetwork错误-1001.)"}由于未捕获的异常"NSInternalInconsistencyException"而终止应用程序,原因:仅在主线程上运行!"***首先抛出调用堆栈:(0 CoreFoundation 0x01cc5946 __exceptionPreprocess + 1821 libobjc.A.dylib 0x0194ea97 objc_exception_throw + 442 CoreFoundation 0x01cc57da + [NSException提高:格式:参数:] + 1383 Foundation 0x015c287e-[NSAssertionHandler handleFailureInFunction:file:lineNumber:description:] + 1024 UIFoundation 0x03f2bfc2-[NSLayoutManager(NSPrivate)_resizeTextViewForTextContainer:] + 4185 UIFoundation 0x03f2bcbe-[NSLayoutManager(NSPrivate)_recalculateUsageForTextContainerAtIndex:] + 20176 UIFoundation 0x03f659e4-[NSLayoutManager textStorage:edited:range:changeInLength:invalidatedRange:] + 8717 UIFoundation 0x03f65af4-[NSLayoutManager processEditingForTextStorage:edited:range:changeInLength:invalidatedRange:] + 858 UIFoundation 0x03f8f9eb-[NSTextStorage _notifyEdited:range:changeInLength:invalidatedRange:] + 1539 UIFoundation 0x03f8f50a-[NSTextStorage processEditing] + 45810 UIFoundation 0x03f8f094-[NSTextStorage endEditing] + 8011 UIFoundation 0x03f8f11f-[NSTextStorage坐标] + 6712 UIKit 0x00b4631e-[UITextView setAttributedText:] + 25013 UIKit 0x00b4c305-[UITextView setText:] + 14914 MYApp 0x0003fe75 __36- [TestApiViewController方法调用API:] _ block_invoke + 19715 MYTESTSDK 0x000d0bb5 __43- [ViewcontrollerA callAPIWithCompletionHandler:] _ block_invoke + 53316 CFNetwork 0x02650c03 __49-[__ NSCFLocalSessionTask _task_onqueue_didFinish] _block_invoke + 34317基础0x015eb715 __NSBLOCKOPERATION_IS_CALLING_OUT_TO_A_BLOCK__ + 1218 Foundation 0x01512165-[NSBlockOperation main] + 9919 Foundation 0x014f1567-[__ NSOperationInternal _start:] + 70020 Foundation 0x014f1299-[NSOperation start] + 8321基础0x014f10e3 __NSOQSchedule_f + 23722 libdispatch.dylib 0x02e22e2f _dispatch_client_callout + 1423 libdispatch.dylib 0x02e08afc _dispatch_queue_drain + 147524 libdispatch.dylib 0x02e083c3 _dispatch_queue_invoke + 21225 libdispatch.dylib 0x02e0b067 _dispatch_root_queue_drain + 46626 libdispatch.dylib 0x02e0c84a _dispatch_worker_thread3 + 11527 libsystem_pthread.dylib 0x0317e296 _pthread_wqthread + 72428 libsystem_pthread.dylib 0x0317beea start_wqthread + 30)libc ++ abi.dylib:以类型为NSException的未捕获异常终止(lldb) 

解决方案

您无法在后台线程上执行任何与UI相关的操作.在这里,您尝试在后台线程上的textview中设置文本.这就是为什么在主线程上运行异常警报的原因.您可以按如下所示在主线程上更新textview文本.

尝试使用此代码设置文本:

  dispatch_async(dispatch_get_main_queue(),^ {NSLog(@"viewcontroller response =%@",responseString);txt_output.text = [NSString stringWithFormat:@%@",strResponse];}); 

With reference this question answer

i am trying to show output string in textview but its kill it. please check

NSLog(@"viewcontroller response =%@",responseString); 
txt_output.text=[NSString stringWithFormat:@"%@",strResponse]; 

txt_output is in my ViewcontrollerB, and i am returning from ViewcontrollerA

NSString *returnstring=[NSString stringWithFormat:@"request=%@ responstring=%@ error=%@",request,responseString,error]; completionHandler(responseString);

Resonse print on log , but after it kills while showing on textview

viewcontroller response =request=<NSMutableURLRequest: 0x786b1b30> { URL: myhosturl } responstring= error=Error Domain=NSURLErrorDomain Code=-1001 "The operation couldn’t be completed. (NSURLErrorDomain error -1001.)" UserInfo=0x786bb400 {NSErrorFailingURLKey=http://myhosturl; NSErrorFailingURLStringKey= myhosturl, NSUnderlyingError=0x7a1790d0 "The operation couldn’t be completed. (kCFErrorDomainCFNetwork error -1001.)"}

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Only run on the main thread!'
*** First throw call stack:
(
    0   CoreFoundation                      0x01cc5946 __exceptionPreprocess + 182
    1   libobjc.A.dylib                     0x0194ea97 objc_exception_throw + 44
    2   CoreFoundation                      0x01cc57da +[NSException raise:format:arguments:] + 138
    3   Foundation                          0x015c287e -[NSAssertionHandler handleFailureInFunction:file:lineNumber:description:] + 102
    4   UIFoundation                        0x03f2bfc2 -[NSLayoutManager(NSPrivate) _resizeTextViewForTextContainer:] + 418
    5   UIFoundation                        0x03f2bcbe -[NSLayoutManager(NSPrivate) _recalculateUsageForTextContainerAtIndex:] + 2017
    6   UIFoundation                        0x03f659e4 -[NSLayoutManager textStorage:edited:range:changeInLength:invalidatedRange:] + 871
    7   UIFoundation                        0x03f65af4 -[NSLayoutManager processEditingForTextStorage:edited:range:changeInLength:invalidatedRange:] + 85
    8   UIFoundation                        0x03f8f9eb -[NSTextStorage _notifyEdited:range:changeInLength:invalidatedRange:] + 153
    9   UIFoundation                        0x03f8f50a -[NSTextStorage processEditing] + 458
    10  UIFoundation                        0x03f8f094 -[NSTextStorage endEditing] + 80
    11  UIFoundation                        0x03f8f11f -[NSTextStorage coordinateEditing:] + 67
    12  UIKit                               0x00b4631e -[UITextView setAttributedText:] + 250
    13  UIKit                               0x00b4c305 -[UITextView setText:] + 149
    14  MYApp                           0x0003fe75 __36-[TestApiViewController methodcallAPI:]_block_invoke + 197
    15  MYTESTSDK                        0x000d0bb5 __43-[ViewcontrollerA callAPIWithCompletionHandler:]_block_invoke + 533
    16  CFNetwork                           0x02650c03 __49-[__NSCFLocalSessionTask _task_onqueue_didFinish]_block_invoke + 343
    17  Foundation                          0x015eb715 __NSBLOCKOPERATION_IS_CALLING_OUT_TO_A_BLOCK__ + 12
    18  Foundation                          0x01512165 -[NSBlockOperation main] + 99
    19  Foundation                          0x014f1567 -[__NSOperationInternal _start:] + 700
    20  Foundation                          0x014f1299 -[NSOperation start] + 83
    21  Foundation                          0x014f10e3 __NSOQSchedule_f + 237
    22  libdispatch.dylib                   0x02e22e2f _dispatch_client_callout + 14
    23  libdispatch.dylib                   0x02e08afc _dispatch_queue_drain + 1475
    24  libdispatch.dylib                   0x02e083c3 _dispatch_queue_invoke + 212
    25  libdispatch.dylib                   0x02e0b067 _dispatch_root_queue_drain + 466
    26  libdispatch.dylib                   0x02e0c84a _dispatch_worker_thread3 + 115
    27  libsystem_pthread.dylib             0x0317e296 _pthread_wqthread + 724
    28  libsystem_pthread.dylib             0x0317beea start_wqthread + 30
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 

解决方案

You can not perform any UI related operations on background threads. Here you are trying to set text in textview on background thread. Thats why exception alerting to run it on main thread. You can update textview text on main thread as given below.

Try to set text using this code :

dispatch_async(dispatch_get_main_queue(), ^{

    NSLog(@"viewcontroller response =%@",responseString);
    txt_output.text=[NSString stringWithFormat:@"%@",strResponse];

});

这篇关于在UITextview中显示响应时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆