我的项目中有三个视图控制器。我们称之为view1,view2和view3。使用来自界面构建器的segue从View1加载View2。但是view3需要以编程方式从view2加载,因为view2向REST服务器发出RESTful请求,并且该请求是异步的。我有一个单独的类来处理REST请求。一旦我从REST服务器获得返回值,我就调用view2的loadResultViewController方法。但是我得到了" libc ++ abi.dylib:以NSException类型的未捕获异常终止"错误。我该如何解决这个问题?
这是我的loadResultViewController方法
func loadResultViewController(summary: String) {
let resultViewController = self.storyboard!.instantiateViewControllerWithIdentifier("ResultView") as! ResultViewController
self.presentViewController(resultViewController, animated: true, completion: nil)
resultViewController.summary.text = summary
}
编辑:
这是完整的错误消息。
2015-09-14 14:01:13.645 My Strategic [5307:791826] *断言失败 - [UIKeyboardTaskQueue waitUntilAllTasksAreFinished],/ SourceCache / UIKit_Sim / UIKit-3347.44.2 / Keyboard / UIKeyboardTaskQueue.m: 374 2015-09-14 14:01:13.678我的战略[5307:791826] * 由于未捕获的异常终止应用程序' NSInternalInconsistencyException',原因:' - [UIKeyboardTaskQueue waitUntilAllTasksAreFinished]可能只从主线程中调用。' ***第一次抛出调用堆栈: ( 0 CoreFoundation 0x000000010345dc65 exceptionPreprocess + 165 1 libobjc.A.dylib 0x0000000104fc8bb7 objc_exception_throw + 45 2 CoreFoundation 0x000000010345daca + [NSException raise:format:arguments:] + 106 3基金会0x00000001038fa98f - [NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 195 4 UIKit 0x00000001044547d6 - [UIKeyboardTaskQueue waitUntilAllTasksAreFinished] + 151 5 UIKit 0x0000000103ef5912 - [UIKeyboardImpl setDelegate:force:] + 473 6 UIKit 0x00000001041a04ad - [UIPeripheralHost(UIKitInternal)_reloadInputViewsForResponder:] + 1002 7 UIKit 0x00000001041a8834 - [UIPeripheralHost(UIKitInternal)_preserveInputViewsWithId:animated:reset:] + 504 8 UIKit 0x0000000103e384f1 - [UIViewController _presentViewController:modalSourceViewController:presentationController:animationController:interactionController:completion:] + 623 9 UIKit 0x0000000103e3976e - [UIViewController _presentViewController:withAnimationController:completion:] + 3079 10 UIKit 0x0000000103e3b6c1 __62- [UIViewController presentViewController:animated:completion:] _ block_invoke + 132 11 UIKit 0x0000000103e3b5e5 - [UIViewController presentViewController:animated:completion:] + 229 12我的战略0x0000000102dbfc26 _TFC14My_Strategic22PostCodeViewController24loadResultViewControllerfS0_FSST_ + 870 13我的战略0x0000000102dc0cae _TTWC14My_Strategic22PostCodeViewControllerS_12RestDelegateS_FS1_24loadResultViewControllerUS1___fQPS1_FSST_ + 94 14我的战略0x0000000102dcfcf0 _TFFC14My_Strategic4Rest9getRidingFS0_FSST_U_FTGSQCSo13NSURLResponse_GSQCSo6NSData_GSQCSo7NSError__T_ + 2960 15我的战略性0x0000000102dcfe4a _TTRXFo_oGSQCSo13NSURLResponse_oGSQCSo6NSData_oGSQCSo7NSError__dT__XFdCb_dGSQS__dGSQS0__dGSQS1 ___ dT + 90 16 CFNetwork 0x000000010566d8c5 67+ [NSURLConnection sendAsynchronousRequest:queue:completionHandler:] _ block_invoke_2 + 155 17基金会0x000000010391e57f __NSBLOCKOPERATION_IS_CALLING_OUT_TO_A_BLOCK + 7 18基金会0x000000010385f0b2 - [NSBlockOperation main] + 98 19基金会0x0000000103841774 - [__ NSOperationInternal _start:] + 645 20基金会0x0000000103841383 __NSOQSchedule_f + 184 21 libdispatch.dylib 0x0000000106ac2614 _dispatch_client_callout + 8 22 libdispatch.dylib 0x0000000106aa96a7 _dispatch_queue_drain + 2176 23 libdispatch.dylib 0x0000000106aa8cc0 _dispatch_queue_invoke + 235 24 libdispatch.dylib 0x0000000106aac3b9 _dispatch_root_queue_drain + 1359 25 libdispatch.dylib 0x0000000106aadb17 _dispatch_worker_thread3 + 111 26 libsystem_pthread.dylib 0x0000000106e2f637 _pthread_wqthread + 729 27 libsystem_pthread.dylib 0x0000000106e2d40d start_wqthread + 13 ) libc ++ abi.dylib:以NSException类型的未捕获异常终止
答案 0 :(得分:2)
这是错误消息的相关部分:
原因:' - [UIKeyboardTaskQueue waitUntilAllTasksAreFinished]只能从主线程调用。'
您在后台线程上发出REST请求,然后在获得结果时,在同一个线程上执行转换。所有UI代码通常应该在主线程上运行。
尝试将代码包装在方法体中:
dispatch_async(dispatch_get_main_queue(),{
// your code here
})
答案 1 :(得分:0)
建议中的小错误..表达式块的前缀应为'^',如下所示
dispatch_async(dispatch_get_main_queue(), ^{
// your code here
})