我为http GET请求编写了一些代码。在运行时,我无法完全了解正在逐行进行的操作。如果有人可以用拉面的术语向我解释代码,我将不胜感激。
该代码具有功能性(我认为)。我只需要真正解释一下与代码一致的内容。
func httpGetUrl(baseUrl: String,parameters:[String:String]) -> String {
var response:String=baseUrl
var i:Int=1
if(parameters.count>0)
{
response=response+"?"
for (key, value) in parameters {
let k:String=key.replacingOccurrences(of: " ", with: "")
let v:String=value.replacingOccurrences(of: " ", with: "")
if(i<parameters.count)
{
response=response+k+"="+v+"&"
}
else
{
response=response+k+"="+v
}
i+=1
}
}
return response
}
let url1 = httpGetUrl(baseUrl: "http://boardgames.com/game", parameters:
["Genre":"Strategy", "name":"Settlers Of Catan", "User Rating":"high"])
let url2 = httpGetUrl(baseUrl: "http://boardgames.com/game", parameters:
[:])
//
//// Sample Output: query params can be in any order
print(url1) // "http://boardgames.com/game?
genre=strategy&name=settlersofcatan&userrating=high"
print(url2) // "http://boardgames.com/game"