快速代码崩溃:"无法识别的选择器发送到实例"

时间:2017-04-12 18:50:52

标签: swift

我试图编写一个发出HTTP请求的命令行Swift应用程序。作为旁注,我想为应用程序的title func提供一个会话,以便我可以使用依赖注入来对代码进行单元测试(因此我定义的协议)。

这与发送到实例的"无法识别的选择器崩溃。"这是什么意思?

run

完整的崩溃在这里:

//
//  main.swift
//  MockIt
//
//

import Foundation

public protocol URLSessionProtocol {
    func synchronousDataTask( _ url: URL ) -> ( Data?, URLResponse?, Error? )
}

class MyUrlSession : URLSession, URLSessionProtocol {

    func synchronousDataTask( _ url: URL ) -> ( Data?, URLResponse?, Error? ) {
        var data: Data?
        var response: URLResponse?
        var error: Error?

        let semaphore = DispatchSemaphore(value: 0)

        let dataTask = self.dataTask(with: url) {
            data = $0
            response = $1
            error = $2

            semaphore.signal()
        }
        dataTask.resume()

        _ = semaphore.wait(timeout: .distantFuture)

        return (data, response, error)
    }

}

class MyModule {
    func run( _ session: URLSessionProtocol, _ urlString: String ) {
        let url = URL(string: urlString)
        let ( _, response, _ ) =
            session.synchronousDataTask(url!)
        print( response! )
    }
}


MyModule().run( MyUrlSession(), "https://myapiendpoint.com" )

不确定是否重要,但这是我的app目录的一般结构:

2017-04-12 11:29:07.930076 MockIt[33267:1871173] -[MockIt.MyUrlSession dataTaskForRequest:completion:]: unrecognized selector sent to instance 0x100a043d0
2017-04-12 11:29:07.933897 MockIt[33267:1871173] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[MockIt.MyUrlSession dataTaskForRequest:completion:]: unrecognized selector sent to instance 0x100a043d0'
*** First throw call stack:
(
    0   CoreFoundation                      0x00007fff785dfe7b __exceptionPreprocess + 171
    1   libobjc.A.dylib                     0x00007fff8d1c0cad objc_exception_throw + 48
    2   CoreFoundation                      0x00007fff78661cb4 -[NSObject(NSObject) doesNotRecognizeSelector:] + 132
    3   CoreFoundation                      0x00007fff78551fb5 ___forwarding___ + 1061
    4   CoreFoundation                      0x00007fff78551b08 _CF_forwarding_prep_0 + 120
    5   MockIt                              0x000000010000177d _TFC6MockIt12MyUrlSession19synchronousDataTaskfV10Foundation3URLTGSqVS1_4Data_GSqCSo11URLResponse_GSqPs5Error___ + 477
    6   MockIt                              0x0000000100001bf8 _TTWC6MockIt12MyUrlSessionS_18URLSessionProtocolS_FS1_19synchronousDataTaskfV10Foundation3URLTGSqVS2_4Data_GSqCSo11URLResponse_GSqPs5Error___ + 72
    7   MockIt                              0x0000000100001d1d _TFC6MockIt8MyModule3runfTPS_18URLSessionProtocol_SS_T_ + 253
    8   MockIt                              0x0000000100001584 main + 116
    9   libdyld.dylib                       0x00007fff8daa4255 start + 1
    10  ???                                 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 

1 个答案:

答案 0 :(得分:1)

stderr并非设计为子类。

我认为您遇到的问题源于您没有完全初始化err.err子类。当URLSession意图通过URLSession至少使用配置对象进行初始化时,您正在调用init()

您无法从子类中自行调用这些初始化程序,因为它们已被特别标记为不可继承。

您无法自行分配配置属性,因为它是正确的只读。

你可以通过覆盖属性来欺骗自己,但是不能将URLSession子类化为更好的主意。您是否考虑将此作为扩展名?

this question中有更多信息。