我开始在iOS的Swift(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()
}
}
然后,当我按下按钮时,屏幕上没有显示任何内容,并出现以下错误:线程1:信号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)
答案 0 :(得分:0)
您可以使用URLSession.shared或以默认配置实例化URLSession:
func checkEndpoint(){
let config = URLSessionConfiguration.default
let session = URLSession(configuration: config)
let url = URL(string: "https://www.google.com")!
let task = session.dataTask(with: url) { (data, response, error) in
print(response)
}
task.resume()
}