URLRequestConvertible的Swift协议见证

时间:2016-02-24 17:42:11

标签: ios swift alamofire nsurlrequest

我有一个路由器助手功能,用于在应用程序中进行所有REST调用。我使用Alamofire进行REST调用。最近发布的应用程序遇到了一些我无法理解的崩溃。在beta测试期间,此崩溃从未显示过。在组织者中,崩溃日志显示该线程:

0    MyApp       Router.URLRequest.getter
1    MyApp       protocol witness for URLRequestConvertible.URLRequest.getter in conformance Router
2    Alamofire   request(URLRequestConvertible) -> Request
...

helper类生成一个URLRequestConvertible对象,用于REST调用。

enum Router: URLRequestConvertible {

    case Get(query: String, params: [String: AnyObject]?)
    case Post(query: String, params: [String: AnyObject]?)
    case Put(query: String, params: [String: AnyObject]?, encoding: ParameterEncoding)
    case Delete(query: String, params: [String: AnyObject]?)

    var URLRequest: NSMutableURLRequest {
        var encodeMethod: Alamofire.ParameterEncoding = Alamofire.ParameterEncoding.JSON

        // Default to GET
        var httpMethod: String = Alamofire.Method.GET.rawValue

        let (path, parameters): (String, [String: AnyObject]?) = {
            switch self {
            case .Get(let query, let params):
                // Set the request call
                httpMethod = Alamofire.Method.GET.rawValue
                // Return the query
                return (query, params)
            case .Post(let query, let params):
                // Set the request call
                httpMethod = Alamofire.Method.POST.rawValue
                // Return the query
                return (query, params)
            case .Put(let query, let params, let encoding):
                // Set the request call
                httpMethod = Alamofire.Method.PUT.rawValue
                // Set the encoding
                encodeMethod = encoding
                // Return the query
                return (query, params)
            case .Delete(let query, let params):
                // Set the request call
                httpMethod = Alamofire.Method.DELETE.rawValue
                // Return the query
                return (query, params)
            }
            }()


        // Create the URL Request
        let URLRequest = NSMutableURLRequest(URL: NSURL(string: Globals.BASE_URL + path)!)
        // set header fields
        if let key = NSUserDefaults.standardUserDefaults().stringForKey(Globals.NS_KEY_SESSION) {
            URLRequest.setValue(key, forHTTPHeaderField: "X-XX-XXX")
        }
        // Add user agent
        if let userAgent = NSUserDefaults.standardUserDefaults().stringForKey(Globals.NS_KEY_USER_AGENT) {
            URLRequest.setValue(userAgent, forHTTPHeaderField: "User-Agent")
        }

        // Set the HTTP method
        URLRequest.HTTPMethod = httpMethod

        return encodeMethod.encode(URLRequest, parameters: parameters).0
    }
}

打开堆栈跟踪时,崩溃所指向的行是

let URLRequest = NSMutableURLRequest(URL: NSURL(string: Globals.BASE_URL + path)!)

其中Globals.BASE_URL是静态常量值。

有谁知道导致此次崩溃的原因是什么?

0 个答案:

没有答案