0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

[iOS] [Swift] クエリ文字列(URLパラメーター)の作成

Posted at

辞書 → クエリ文字列(URLパラメーター)にコンバート

extension Dictionary where Key : StringProtocol, Value : StringProtocol {
    var queryString: String {
        var components = URLComponents()
        components.queryItems = self.map {
            URLQueryItem(name: $0 as! String, value: String(describing: $1))
        }
       return (components.url?.absoluteString)!
    }
}

使用法

let queryDic = ["key1": "value1", "key2": "value2"]
print(queryDic.queryString)
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?