如何使用 swift 在字典中添加多个参数 [String,Dictionary]?
How to add multiple paramaters[String,Dictionary] in a dictionary using swift?
我正在使用 Swift.I 构建 iOS 应用程序,我正在使用 XCODE 版本 6.3。
我想 post 使用 JSON.For 将数据发送到服务器,我创建了一个字典来执行以下操作。
//This is my dictionary right now
var parameters = ["authenticity_token":name,"platform":"mobileApp", "email": userEmail, "password": userPassword,"remember_me":"1"]
request.HTTPBody = NSJSONSerialization.dataWithJSONObject(parameters, options: nil, error: &err)
我想按如下所示的格式传递数据。
{"authenticity_token":"token","platform":"mobileApp",
"user":{"email":"email_id", "password":"123456",
"remember_me":"1"}}
现在参数字典格式
[platform: mobileApp, authenticity_token: a1oj3olt5jn169LHn59ZbfbrBVUyov7sDVHlOl+2YzE=, email: su@gmail.com, remember_me: 1, password: samrat]
参数的顺序也改变了。
我想传递给我的字典。
或者有没有可能添加两个词典。
这是我 post 将数据发送到 server.I 的代码,是否正确与否还有一个困惑。
func postData()
{
let name = defaults.stringForKey("secretKey")
var parameters = ["authenticity_token":name,"platform":"mobileApp", "email": userEmail, "password": userPassword,"remember_me":"1"]
println("Dic\(parameters)")
let url = NSURL(string: "http://behance.moblearn.net/users/sign_in")
var session = NSURLSession.sharedSession()
let request = NSMutableURLRequest(URL: url!)
request.HTTPMethod = "POST"
var err: NSError?
request.HTTPBody = NSJSONSerialization.dataWithJSONObject(parameters, options: nil, error: &err)
let jsonStr = NSString(data: request.HTTPBody!, encoding: NSUTF8StringEncoding)
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.addValue("application/json", forHTTPHeaderField: "Accept")
println("Post Method reached")
var task = session.dataTaskWithRequest(request, completionHandler: {data, response, error -> Void in
println("Response: \(response)")
var strData = NSString(data: data, encoding: NSUTF8StringEncoding)
println("Body: \(strData)")
var err: NSError?
var json = NSJSONSerialization.JSONObjectWithData(data, options: .MutableLeaves, error: &err) as? NSDictionary
if(err != nil) {
println(err!.localizedDescription)
let jsonStr = NSString(data: data, encoding: NSUTF8StringEncoding)
println("Error could not parse JSON: '\(jsonStr)'")
}
else {
if let parseJSON = json {
var success = parseJSON["success"] as? Int
println("Succes: \(success)")
}
else {
let jsonStr = NSString(data: data, encoding: NSUTF8StringEncoding)
println("Error could not parse JSON: \(jsonStr)")
}
}
})
task.resume()
}
我深陷 trouble.I 我是 swift.Help 的初学者,我们将不胜感激。
这样做:
let innerDict = ["email": "email_id", "password": "123456", "remember_me": "1"]
let mainDict = ["authenticity_token": "token", "platform": "mobileApp", "user": innerDict]
我正在使用 Swift.I 构建 iOS 应用程序,我正在使用 XCODE 版本 6.3。 我想 post 使用 JSON.For 将数据发送到服务器,我创建了一个字典来执行以下操作。
//This is my dictionary right now
var parameters = ["authenticity_token":name,"platform":"mobileApp", "email": userEmail, "password": userPassword,"remember_me":"1"]
request.HTTPBody = NSJSONSerialization.dataWithJSONObject(parameters, options: nil, error: &err)
我想按如下所示的格式传递数据。
{"authenticity_token":"token","platform":"mobileApp",
"user":{"email":"email_id", "password":"123456",
"remember_me":"1"}}
现在参数字典格式
[platform: mobileApp, authenticity_token: a1oj3olt5jn169LHn59ZbfbrBVUyov7sDVHlOl+2YzE=, email: su@gmail.com, remember_me: 1, password: samrat]
参数的顺序也改变了。 我想传递给我的字典。 或者有没有可能添加两个词典。
这是我 post 将数据发送到 server.I 的代码,是否正确与否还有一个困惑。
func postData()
{
let name = defaults.stringForKey("secretKey")
var parameters = ["authenticity_token":name,"platform":"mobileApp", "email": userEmail, "password": userPassword,"remember_me":"1"]
println("Dic\(parameters)")
let url = NSURL(string: "http://behance.moblearn.net/users/sign_in")
var session = NSURLSession.sharedSession()
let request = NSMutableURLRequest(URL: url!)
request.HTTPMethod = "POST"
var err: NSError?
request.HTTPBody = NSJSONSerialization.dataWithJSONObject(parameters, options: nil, error: &err)
let jsonStr = NSString(data: request.HTTPBody!, encoding: NSUTF8StringEncoding)
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.addValue("application/json", forHTTPHeaderField: "Accept")
println("Post Method reached")
var task = session.dataTaskWithRequest(request, completionHandler: {data, response, error -> Void in
println("Response: \(response)")
var strData = NSString(data: data, encoding: NSUTF8StringEncoding)
println("Body: \(strData)")
var err: NSError?
var json = NSJSONSerialization.JSONObjectWithData(data, options: .MutableLeaves, error: &err) as? NSDictionary
if(err != nil) {
println(err!.localizedDescription)
let jsonStr = NSString(data: data, encoding: NSUTF8StringEncoding)
println("Error could not parse JSON: '\(jsonStr)'")
}
else {
if let parseJSON = json {
var success = parseJSON["success"] as? Int
println("Succes: \(success)")
}
else {
let jsonStr = NSString(data: data, encoding: NSUTF8StringEncoding)
println("Error could not parse JSON: \(jsonStr)")
}
}
})
task.resume()
}
我深陷 trouble.I 我是 swift.Help 的初学者,我们将不胜感激。
这样做:
let innerDict = ["email": "email_id", "password": "123456", "remember_me": "1"]
let mainDict = ["authenticity_token": "token", "platform": "mobileApp", "user": innerDict]