-1

I'm currently developing my first iOS app using Swift 2.0 and Xcode Beta 2. It reads an external JSON and generates a list in a table view with the data. However, I'm getting a strange little error that I can't seem to fix: error coming in this code for post data server using swift (extra argument error in cal)

@IBAction func registerbutton(sender: AnyObject) {
    func application(application: UIApplication!, didFinishLaunchingWithOptions launchOptions: NSDictionary!) -> Bool {
    //declare parameter as a dictionary which contains string as key and value combination.
    var parameters = ["name": Name.text!, "emailaddress": EmailAddress.text!, "phonenumber": PhoneNumber.text!, "Dealerloaction": dealerlocationtextfield.text!] as Dictionary<String, String>

    //create the url with NSURL
    let url = NSURL(string: "http://192.168.1.75:3002/users/sign_in") //change the url

    //create the session object
    var session = NSURLSession.sharedSession()

    //now create the NSMutableRequest object using the url object
    let request = NSMutableURLRequest(URL: url!)
    request.HTTPMethod = "POST" //set http method as POST

    var err: NSError?
    request.HTTPBody = NSJSONSerialization.dataWithJSONObject(parameters, options: nil, error: &err ) // pass dictionary to nsdata object and set it as request body

    request.addValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
    request.addValue("application/json", forHTTPHeaderField: "Accept")

    //create dataTask using the session object to send data to the server
    var task = session.dataTaskWithRequest(request, completionHandler: {data, response, error -> Void in
        print("Response: \(response)")
        var strData = NSString(data: data!, encoding: NSUTF8StringEncoding)
        print("Body: \(strData)")
        var err: NSError?
        var json = NSJSONSerialization.JSONObjectWithData(data, options: .MutableLeaves, error: &err) as? NSDictionary

        // Did the JSONObjectWithData constructor return an error? If so, log the error to the console
        if(err != nil) {
            print(err!.localizedDescription)
            let jsonStr = NSString(data: data!, encoding: NSUTF8StringEncoding)
            print("Error could not parse JSON: '\(jsonStr)'")
        }
        else {
            // The JSONObjectWithData constructor didn't return an error. But, we should still
            // check and make sure that json has a value using optional binding.
            if let parseJSON = json {
                // Okay, the parsedJSON is here, let's get the value for 'success' out of it
                var success = parseJSON["success"] as? Int
                println("Succes: \(success)")
            }
            else {
                // Woa, okay the json object was nil, something went worng. Maybe the server isn't running?
                let jsonStr = NSString(data: data!, encoding: NSUTF8StringEncoding)
                print("Error could not parse JSON: \(jsonStr)")
            }
        }
    })

    task.resume()
        return true
}
lee
  • 11
  • 1

1 Answers1

1

Error handling in Swift differs than error handling in Objective-C. In Swift the NSJSONSerialization.dataWithJSONObject doesn't have a parameter for error. It actually throws an exception and conform to the new error handling system.

itskoBits
  • 435
  • 4
  • 13