Created
November 3, 2017 02:37
-
-
Save ryantxr/6e89d4bbe97760da354ac7569351f5ab to your computer and use it in GitHub Desktop.
Extension for [String : String] to convert to http arg string
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Extensions.swift | |
// ProcessCompletion | |
// | |
// Created by ryan teixeira on 11/2/17. | |
// Copyright © 2017 Blazecore. All rights reserved. | |
// | |
import Foundation | |
/* | |
An extension for [String : String] to convert it to a string of key=value pairs | |
suitable for use as a parameter string for a url. | |
let params = ["key1" : "value1", "key2" : "value2", "key3" : "value3"] | |
let argStr = params.toHttpArgString() | |
key1=value1&key2=value2&key3=value3 | |
*/ | |
protocol ArgType {} | |
extension String: ArgType {} | |
extension Dictionary where Key: ArgType, Value: ArgType { | |
// Implement using a loop | |
func toHttpArgString() -> String { | |
var r = String() | |
for (n, v) in self { | |
if !r.isEmpty { r += "&" } | |
r += "\(n)=\(v)" | |
} | |
return r | |
} | |
} | |
//params.toHttpArgString() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment