【发布时间】:2018-06-25 23:09:11
【问题描述】:
我开始使用 Swift for iOS (Xcode 9) 进行开发。我需要制作一个非常简单的应用程序来检查 URL 是否可用。首先,我正在尝试:
import UIKit
import Foundation
class ViewController: UIViewController {
@IBAction func checkEndpoints(_ sender: UIButton) {
checkEndpoint()
}
func checkEndpoint(){
let session = URLSession()
let url = URL(string: "https://www.google.com")!
let task = session.dataTask(with: url) { (data, response, error) in
print(response)
}
task.resume()
}
}
然后,当我按下按钮时,我没有在屏幕上看到任何结果,我收到此错误:Thread 1: signal SIGARBT
知道如何改进此代码吗?欢迎任何理解和学习 Swift 的问题。谢谢!
输出的更多信息:
2018-06-25 21:45:26.859103-0300 Check enpoints test[67554:3991300] -[NSURLSession dataTaskForRequest:completion:]: unrecognized selector sent to instance 0x604000015140
2018-06-25 21:45:26.865855-0300 Check enpoints test[67554:3991300] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSURLSession dataTaskForRequest:completion:]: unrecognized selector sent to instance 0x604000015140'
*** First throw call stack:
(
0 CoreFoundation 0x000000010f1b31e6 __exceptionPreprocess + 294
1 libobjc.A.dylib 0x000000010b5ba031 objc_exception_throw + 48
2 CoreFoundation 0x000000010f234784 -[NSObject(NSObject) doesNotRecognizeSelector:] + 132
3 CoreFoundation 0x000000010f135898 ___forwarding___ + 1432
4 CoreFoundation 0x000000010f135278 _CF_forwarding_prep_0 + 120
5 Check enpoints test 0x000000010acb052b _T019Check_enpoints_test14ViewControllerC014checkEndpoint_C0yyF + 427
6 Check enpoints test 0x000000010acb031b _T019Check_enpoints_test14ViewControllerC14checkEndpointsySo8UIButtonCF + 43
7 Check enpoints test 0x000000010acb036c _T019Check_enpoints_test14ViewControllerC14checkEndpointsySo8UIButtonCFTo + 60
8 UIKit 0x000000010be573e8 -[UIApplication sendAction:to:from:forEvent:] + 83
9 UIKit 0x000000010bfd27a4 -[UIControl sendAction:to:forEvent:] + 67
10 UIKit 0x000000010bfd2ac1 -[UIControl _sendActionsForEvents:withEvent:] + 450
11 UIKit 0x000000010bfd1a09 -[UIControl touchesEnded:withEvent:] + 580
12 UIKit 0x000000010becc0bf -[UIWindow _sendTouchesForEvent:] + 2729
13 UIKit 0x000000010becd7c1 -[UIWindow sendEvent:] + 4086
14 UIKit 0x000000010be71310 -[UIApplication sendEvent:] + 352
15 UIKit 0x000000010c7b26af __dispatchPreprocessedEventFromEventQueue + 2796
16 UIKit 0x000000010c7b52c4 __handleEventQueueInternal + 5949
17 CoreFoundation 0x000000010f155bb1 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
18 CoreFoundation 0x000000010f13a4af __CFRunLoopDoSources0 + 271
19 CoreFoundation 0x000000010f139a6f __CFRunLoopRun + 1263
20 CoreFoundation 0x000000010f13930b CFRunLoopRunSpecific + 635
21 GraphicsServices 0x0000000111679a73 GSEventRunModal + 62
22 UIKit 0x000000010be56057 UIApplicationMain + 159
23 Check enpoints test 0x000000010acb1c07 main + 55
24 libdyld.dylib 0x0000000110394955 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
【问题讨论】:
-
尝试使用
URLSession.shared而不是新的URLSession() -
究竟是哪一行导致了错误?
-
在您的 IBAction 函数中注释掉 checkEndPoint 以查看错误是否是由故事板内容链接引起的。如果调用不正确,还要更改函数名称,使其与类中的其他函数不同(这会导致无限循环错误)。
-
Alexander:使用 URLSession.shared 单击按钮后我什么也没得到。 rmaddy:单击模拟应用程序 Xcode 中的按钮后,显示我现在附加的图像。
-
那张图片没有显示崩溃发生的位置。
标签: ios swift uikit urlsession