类型'Void'在枚举结果<t>中没有成员

时间:2018-05-07 06:21:14

标签: swift nsurlsession

我有这段代码:

enum ApiConstans {
    static let BaseUrl = "http://myname.pl"
    static let iosPath = "/pub/ios/"
    static let jsonPath = "json.php"

    static var fullPath: String { return BaseUrl + iosPath + jsonPath }
}


struct Connect {
    enum Result<T> {
        case succes(T)
        case error(String)
    }

    func getJsonFromServer(parameters: String) -> Result<String> {

        let fullUrlString = ApiConstans.fullPath + parameters
        guard let url = URL(string: fullUrlString) else {
            return .error("Error 100: Problem with url")
        }

        URLSession.shared.dataTask(with: url) {  (data, response, error) in
            guard error == nil else {
                return .error("Error 100: Problem with url")
            }

            guard let data = data else {
                return .error("Error 101: Problem with url")
            }

            debugPrint("R> \(fullUrlString)")
            return .succes(data)
        }
    }

    func checkUsersLogin(login: String?, password: String?) ->  Result<String> {
        getJsonFromServer(parameters: "?action=LOGOWANIE&login=\(login!)&password=\(password!)")
    }

}

我在所有回复中都有错误问题:类型'Void'没有成员'错误'而且'Type Void'没有成员'succes'

我可以请你修复这段代码吗?

3 个答案:

答案 0 :(得分:2)

编译器告诉您从异步Result处理程序返回dataTask,该处理程序不会返回您的自定义类型。

您的代码的主要问题是您以同步方式执行异步操作(调用服务器)(立即返回结果)。相反,您应该将回调闭包传递给您在getJsonFromServer处理程序中调用的dataTask

答案 1 :(得分:2)

从服务器接收数据时,应使用closure来返回数据。

typealias completionHandler = (Result<Data >) -> ()

func getJsonFromServer(parameters: String, completion: @escaping completionHandler) {
    let fullUrlString = ApiConstans.fullPath + parameters
    guard let url = URL(string: fullUrlString) else {
        return completion(.error("Error 100: Problem with url"))
    }

    URLSession.shared.dataTask(with: url) {  (data, response, error) in
        guard error == nil else {
            return completion(.error("Error 100: Problem with url"))
        }

        guard let data = data else {
            return completion(.error("Error 101: Problem with url"))
        }

        debugPrint("R> \(fullUrlString)")
        return completion(.succes(data))
    }
}

答案 2 :(得分:0)

如果是块,则默认返回类型为Void,因此该返回引用了块返回类型。您需要使用完成处理程序方式才能使其正常工作。

enum ApiConstans {
    static let BaseUrl = "http://myname.pl"
    static let iosPath = "/pub/ios/"
    static let jsonPath = "json.php"

    static var fullPath: String { return BaseUrl + iosPath + jsonPath }
}


struct Connect {
    enum Result<T> {
        case succes(T)
        case error(String)
    }

    func getJsonFromServer(parameters: String, completion: @escaping (Result<String>) -> Void) {

        let fullUrlString = ApiConstans.fullPath + parameters
        guard let url = URL(string: fullUrlString) else {
            completion(Result.error("Error 100: Problem with url"))
            return
        }

        URLSession.shared.dataTask(with: url) {  (data, response, error) in
            guard error == nil else {
                completion(Result.error("Error 100: Problem with url"))
                return
            }

            guard let data = data else {
                completion(Result.error("Error 100: Problem with url"))
                return
            }

            print("R> \(fullUrlString)")
            completion(Result.succes("CONVERT DATA TO STRING USE DATA HERE "))
        }
    }

    func checkUsersLogin(login: String?, password: String?, completion: @escaping (Result<String>) -> Void) {
        self.getJsonFromServer(parameters: "?action=LOGOWANIE&login=\(login!)&password=\(password!)", completion: completion)
    }

}

你可以通过完成,你也必须等待API完成,所以我建议使用处理程序技术来正确流动。