接收Promise <void>而不是NSMutableURLRequest

时间:2018-11-03 23:51:46

标签: ios swift nsmutableurlrequest promisekit

我正在尝试登录我的应用,但是,我所有的APIRequest都显示为Void而不是APIRequest。此功能在Swift 2中可以使用,此后进行了一些小的更改,以尝试从PromiseKit 4迁移到PromiseKit6。

该功能确实涉及很多其他内容,如果需要更多内容,请发表评论。给我错误的函数是:

class func recover403(error: Error) -> Promise<NSData> {
        if case let APIError.InvalidToken(request) = error {
            // Log in, then continue the promise chain
            let login = RootViewController.sharedInstance.showLoginController()
            return login.then { request -> Promise<NSData> in
                return API.transmit(request: request as! APIRequest)
            }
        } else if case CocoaError.userActivityConnectionUnavailable = error {

            let vc = ErrorViewController(nibName: "ErrorViewController", bundle: nil)
            let actions = ["Try Again"]

            vc.actions = actions
            vc.promise.then { index -> Promise<NSData> in
                RootViewController.sharedInstance.popAlertViewController()
                return API.transmit(request: request as! APIRequest)
            }.catch({ (Error) in
                // No catch
            })

            RootViewController.sharedInstance.pushAlertViewController(nvc: vc)
            vc.errorLabel.text = "Please check your network connection and try again!"
        }
        return Promise<NSData>(error: error)
    }

“返回API.transmit(request:请求为!APIRequest)”正在抛出

Error: Value of tuple type 'Void' has no member 'value'
Warning: Cast from 'Void' to unrelated type 'APIRequest' always fails

第二个“ return API.transmit(request:request as!APIRequest)”正在抛出

Error: Generic parameter 'T' could not be inferred
Warning: Cast from '(String, Any?) -> Promise<_>' to unrelated type 'APIRequest' always fails

这是APIRequest.swift

import Foundation

class APIRequest: NSMutableURLRequest {

    var errorConditions: [APIRequestErrorCondition] = []

    init(_ path: String, JSON: Any? = nil) {
        let URL = NSURL(string: API.baseURL + path)!
        super.init(url: URL as URL, cachePolicy: .useProtocolCachePolicy, timeoutInterval: 60)

        // Settings for both GET and POST
        setValue(API.agent, forHTTPHeaderField: "User-Agent")
        setValue("application/json", forHTTPHeaderField: "Accept")

        // Settings for POST, which is assumed when JSON != nil
        if let JSON = JSON {
            httpMethod = "POST"
            httpBody = try! JSONSerialization.data(withJSONObject: JSON, options: [])
            setValue("application/json", forHTTPHeaderField: "Content-Type")
        }
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}

最后是传输功能:

class func transmit(request: APIRequest) -> Promise<NSData> {
    request.auth()
    return Promise<NSData> { seal in
        let task = URLSession.shared.dataTask(with: request as URLRequest, completionHandler: { (_data, _response, _error) in
            if let data = _data {
                seal.fulfill(data as NSData)
            } else if let error = _error {
                if case URLError.badServerResponse = error, let rsp = _response as? HTTPURLResponse {
                    seal.reject(BadUrlError.badResponse(rsp.statusCode))
                } else {
                    seal.reject(error)
                }
            } else {
                seal.reject(PMKError.invalidCallingConvention)
            }
            seal.fulfill(_data! as NSData)
        })
        task.resume()
    }
}

此错误涉及很多内容,因此请在评论中告知我是否缺少您需要查看的部分!

添加了:“请求”声明

class func request<T>(path: String, JSON: Any? = nil) -> Promise<T> {
    let request = APIRequest(path, JSON: JSON)
    request.errorConditions = [API.InvalidTokenCondition, API.BadResponseCodeCondition]
    return API.promise(request: request)
}

添加了“登录”声明

func showLoginController() -> Promise<Void> {
    if let vc = modalController.vcs.last as? LoginViewController {
        return vc.promise
    }
    let vc = LoginViewController(nibName: "LoginViewController", bundle: nil)
    pushModalViewController(nvc: vc, animated: true)
    return vc.promise
}

0 个答案:

没有答案