Skip to content

Instantly share code, notes, and snippets.

@KentarouKanno
Last active August 6, 2016 14:14
Show Gist options
  • Save KentarouKanno/d82ed8f71d12d5e68323179fbec991f5 to your computer and use it in GitHub Desktop.
Save KentarouKanno/d82ed8f71d12d5e68323179fbec991f5 to your computer and use it in GitHub Desktop.
NSURLComponents

NSURLComponents

★ パラメーター付きのNSURLを作成する

func convertURL(array: Dictionary<String, String>, urlString: String) -> NSURL? {
    if let components = NSURLComponents(string:urlString) {
        
        components.queryItems = array.map{ (k: String, v: String) -> NSURLQueryItem in
            return NSURLQueryItem(name: k, value: v)
        }
        return components.URL
    }
    return nil
}

let param = ["名前": "日本語", "name": "Kentarou"]

print(convertURL(param, urlString: "http://example.com/"))
//=> Optional(http://example.com/?%E5%90%8D%E5%89%8D=%E6%97%A5%E6%9C%AC%E8%AA%9E&name=Kentarou)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment