Swift Version?

I am running Xcode 10.3 on a MBP from 2015. Does this limit the version of Swift I can write with? How can I tell which version of Swift I am using?

I am getting an error: Value of type URL has no member 'withQueries' and I think it is because the code is incompatible with the version of Swift I am using?

let baseURL = URL(string: "https://api.nasa.gov/planetary/apod")!

let query: [String: String] = [ "api_key": "DEMO_KEY", "date": "2011-07-13"]

let url = baseURL.withQueries(query)!

let task = URLSession.shared.dataTask(with: url) { (data, response, error) in

    if let data = data, let string = String(data: data, encoding: .utf8) { print(string) }

PlaygroundPage.current.finishExecution()

}

task.resume()

I am getting an error: Value of type URL has no member 'withQueries'

Where did you read that URL had a withQueries func ?

To know Swift version, open the Project > Build settings and search for Swift. But that should not be the cause of the error.

I am running Xcode 10.3 on a MBP from 2015. Does this limit the version of Swift I can write with?

Yes, but not too badly. According to Developer > Support > Xcode, Xcode 10.3 supports Swift 5, which is relatively modern.

I am getting an error Value of type URL has no member 'withQueries' and I think it is because the code is incompatible with the version of Swift I am using?

No, there’s something else going on. URL doesn’t have a withQueries(…) method even with the latest tools (Xcode 13.1). I’m not sure where you got that code from, but it’s likely that whoever wrote it has their own extension on URL to add queries.

With regards your overall task, adding query parameters to a URL, that’s best done using URLComponents. For example:

var uc = URLComponents(string: "https://api.nasa.gov/planetary/apod")!
uc.queryItems = [
    .init(name: "api_key", value: "DEMO_KEY"),
    .init(name: "date", value: "2011-07-13")
]
let url = uc.url!
print(url)
// https://api.nasa.gov/planetary/apod?api_key=DEMO_KEY&date=2011-07-13

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Swift Version?
 
 
Q