func stringWithDictionary(dict: NSDictionary ) -> String {
let allKeys = dict.allKeys
let sortKeys = allKeys.sorted { (a, b) -> Bool in
let sa = a as! String
let sb = b as! String
return sa < sb
}
var resultString: String = ""
for tempKey in sortKeys {
let key = tempKey as! String
var tempValue = dict[key]
if tempValue is NSDictionary {
tempValue = stringWithDictionary(dict: tempValue as! NSDictionary)
}
let value = tempValue as! String
resultString = resultString + "\(key)=\(value)&"
}
if resultString.count > 0 {
resultString.removeLast()
}
print(" \(resultString)")
return resultString
}
使用
var loginParamDic:NSDictionary!
var jsonToStr:NSString!
loginParamDic = [
"cName":"bjq1","Acgloginpwd":"www","dpwd ":"piuq","AdAloginpwd ":"ynhq"]
jsonToStr = stringWithDictionary(dict: loginParamDic) as NSString
print("loginParamDic to & jsonToStr\n (jsonToStr)")
oginParamDic to & jsonToStr
Acgloginpwd=www&AdAloginpwd=1qaz1qa1&loginName=cq136&loginpwd =1qaz1qaz