Swift:将responseString ID保存到String

时间:2015-02-17 01:42:24

标签: ios xcode swift

使用HTTP POST发送此代码会返回一个ID,但我到目前为止无法从responseString中提取ID并将其保存为我在应用程序中自己的String。

我正在考虑使用Alamofire,也许这会让事情变得更容易,但我希望能够使用Swift代码来实现它。任何帮助表示赞赏。



var parseError: NSError?

let date = NSDate()
let timeStamp = date.timeIntervalSince1970
var request = NSMutableURLRequest(URL: NSURL(string: URL)!)
request.HTTPMethod = "POST"


let params = ["name":fullName.text, "number":phoneNumber.text, "email":emailAddress.text, "timeStarted":timeStamp] as Dictionary<String, AnyObject>

request.HTTPBody = NSJSONSerialization.dataWithJSONObject(params, options: nil, error: nil)

request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.addValue("application/json", forHTTPHeaderField: "Accept")
let task = NSURLSession.sharedSession().dataTaskWithRequest(request) {
    data, response, error in
    
    if error != nil {
        println("error=\(error)")
        return
    }
    
    println("response = \(response)")
    
    let responseString = NSString(data: data, encoding: NSUTF8StringEncoding)
    println("responseString = \(responseString)")
    
    let idFromServer = responseString?.valueForKeyPath("id") as String!
    println(idFromServer)
    var dateID = idFromServer
    newUser.setValue(dateID, forKey: "dateID")
&#13;
&#13;
&#13;

0 个答案:

没有答案