无法调用" dataTaskWithURL'使用类型'的参数列表(NSMutableURLRequest,(_,_,_)throws - > _)'

时间:2016-02-20 11:14:25

标签: json swift http request

//Send user data to server
    let myUrl = NSURL(string: "http://localhost/signUp")
    let request = NSMutableURLRequest(URL:myUrl!)
    request.HTTPMethod = "POST"
    let postString = "name =\(userName)&email=\(userEmail)&password=\(userPassword) "
    request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding)


    // task execution & JSON

错误

  

无法调用' dataTaskWithURL'使用类型'的参数列表(NSMutableURLRequest,(_,_,_)throws - > _)'

enter image description here

但是我想用这个参数调用dataTaskWithUrl我该怎么做:

    let task = NSURLSession.sharedSession().dataTaskWithURL(request){
        data, response, error in

        if (error != nil){
            print("error\(error)")
            return
        }

        var err:NSError?

        let json = try NSJSONSerialization.JSONObjectWithData(data!, options: .MutableContainers, error: &err) as? NSDictionary

        if let parseJson = json{
            var resultValue = parseJson["status"] as? String
            print("result\(resultValue)")


            var isUserRegistred:Bool = false
            if (resultValue == "Succes"){
                isUserRegistred = true
            }

            var messageToDisplay:String = parseJson["message"] as String!
            if (!isUserRegistred){
                messageToDisplay = parseJson["message"] as String!
            }

            //Launch asynchrone (different thread)
            dispatch_async(dispatch_get_main_queue(), {
                //Display confirmation message
                let myAlert = UIAlertController(title: "Alert", message:messageToDisplay, preferredStyle: UIAlertControllerStyle.Alert)

                let okAction = UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default){ action in
                    self.dismissViewControllerAnimated(true, completion: nil)
                }
                myAlert.addAction(okAction)
                self.presentViewController(myAlert, animated: true, completion: nil)
            })
        }
    }

    task.resume()
}

0 个答案:

没有答案