简体   繁体   中英

Dictionary Type Shorthand Syntax error with XCode 7 / Swift 2

在此处输入图片说明

XCode 7 / Swift 2 spit out a bunch of errors on this code that is based on the snippet from the Alamofire readme . I know nothing about Swift or iOS development, so any pointers would be appreciated here.

import Foundation
import Alamofire

enum PTVRouter: URLRequestConvertible {
    static let baseUrl = "http://timetableapi.ptv.vic.gov.au/"
    static let devId = "__CENSORED__"
    static let devKey = "__CENSORED__"

    case HealthCheck()

    var URLRequest: NSURLRequest {
        let (path: String, parameters: [String: AnyObject]?) = {
            switch self {
            case .HealthCheck():
                return ("/v2/healthcheck", ["devId": PTVRouter.devId])
            }
        }()

        let URL = NSURL(string: PTVRouter.baseUrl)!
        let URLRequest = NSURLRequest(URL: URL.URLByAppendingPathComponent(path))
        let encoding = Alamofire.ParameterEncoding.URL

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

It's hard to reproduce your problem without downloading Alamofire, but try the following:

enum PTVRouter: URLRequestConvertible {
    static let baseUrl = "http://timetableapi.ptv.vic.gov.au/"
    static let devId = "__CENSORED__"
    static let devKey = "__CENSORED__"

    case HealthCheck

    var URLRequest: NSURLRequest {
        let path: String
        let parameters: [String: AnyObject]

        switch self {
        case .HealthCheck:
            path = "/v2/healthcheck"
            parameters = ["devId": PTVRouter.devId]
        }
        let url = NSURL(string: PTVRouter.baseUrl)!
        let urlRequest = NSURLRequest(URL: url.URLByAppendingPathComponent(path))
        let encoding = Alamofire.ParameterEncoding.URL
        return encoding.encode(urlRequest, parameters: parameters).0
        return urlRequest
    }
}

Things to note:

The technique the example uses, where it creates a temporary closure and runs it immediately (what the let (x,y) = { switch and return }() is doing), is no longer necessary as of Swift 1.2 because deferred initialization of let variables is now possible. (they should really update that example code).

You don't need to put () after the case HealthCheck . The () are for including associated types with the case (in the example, they hold parameters for a query), and in your case, since you don't have any, you don't need the parens.

They declare the parameters variable as optional (presumably because the parameters argument of encoding.encode is optional). This isn't necessary (non-optional arguments get upgraded to optional automatically) and it's generally a bad practice to declare things like arrays and dictionaries as optional – emptiness is usually sufficient, and the optionality can complicate things.

That said, your code looks like it ought to compile without these changes, but see if removing the clutter helps find whatever is actually wrong.

(and a couple of stylistic things, variables should start with lower case, even acronyms like “url”, to avoid them getting confused with types which start in upper case. also I wouldn't use that force unwrap, rather I'd leave it optional, use optional chaining, and fatalError if it's nil at the end but that's just my personal preference)

You are not allowed to specify the types of the tuple (path, parameters) in-place. You have to specify the types somewhere else:

In the beginning of the closure:

let (path, parameters) = {() -> (String, [String:AnyObject]?) in
    switch self {
    case .HealthCheck():
        return ("/v2/healthcheck", ["devId": PTVRouter.devId])
    }
}()

or as the tuple type after their names:

let (path, parameters) : (String, [String:AnyObject]?) = {
    switch self {
    case .HealthCheck():
        return ("/v2/healthcheck", ["devId": PTVRouter.devId])
    }
}()

Note that the syntax is really, really ugly in my opinion and obfuscates the actual working of the code in a big way.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM