ios - SIGABRT 尝试使用 URLSession 加载数据
问题描述
我开始使用 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()
}
}
然后,当我按下按钮时,我没有在屏幕上看到任何结果,我收到此错误:线程 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)
解决方案
您可以使用 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()
}
推荐阅读
- html - 如何使css“绘制”响应
- node.js - Nodejs Fetch API:无法验证第一个证书
- python - 如果列包含列表中的字符串,则如何/在 df 中创建新列的最佳方法对两者都使用小写
- python - 为什么这个正则表达式只给出第三个八位字节?
- bootstrap-4 - 如何控制 Bootstrap btn-group(flex-wrap)在哪里中断到新行?
- python - 如何从使用 BeautifulSoup 抓取的列表中删除标签?
- php - PHP:通过 URL 参数在两个字符串之间进行 XOR 的 eval()
- sap - #Webi-Business Objects 中的多值聚合错误
- kubernetes - HDFS 名称节点未在 Kubernetes 上正确显示数据节点列表
- amazon-web-services - 创建 eks 集群时 terraform 上的 ARN 错误无效