-
Notifications
You must be signed in to change notification settings - Fork 23
/
Contents.swift
34 lines (26 loc) · 908 Bytes
/
Contents.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import UIKit
import PlaygroundSupport
//Playground still works without the need to set needsIndefiniteExecution to true
//PlaygroundPage.current.needsIndefiniteExecution = true
extension URL {
func withQueries(_ queries:[String: String]) -> URL? {
var components = URLComponents(url: self, resolvingAgainstBaseURL: true)
components?.queryItems = queries.map {
URLQueryItem(name: $0.0, value: $0.1)
}
return components?.url
}
}
let query: [String: String] = [
"term": "adele",
"media": "music"
]
let baseURL = URL(string: "https://itunes.apple.com/search?")!
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()