SIGABRT 尝试使用 URLSession 加载数据

SIGABRT trying to load data using a URLSession

我在 Swift 开始开发 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。谢谢!

PD:在模拟应用程序上单击按钮后出现错误的图片。

输出的更多信息:

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()
}