NewBeginnings
NewBeginnings

Reputation: 347

Getting NSData statement errors in new swift 2.0

I was working on a project in which I wanted to pass user data to online database through JSON and PHP. This code generated no errors when I was working in XCode 6 (older versions) of swift, however now it's generating the error

'(NSData!, NSURLResponse!, NSError!) -> Void' is not convertible to '(NSData?, NSURLResponse?, NSError?) -> Void'

I suppose it's a syntax error with the new optional and forced type operator in swift.

Here is my code:

@IBAction func webserviceregistration() {
    var timeFormatter = NSDateFormatter()
    timeFormatter.timeStyle = NSDateFormatterStyle.ShortStyle
    var strDate = timeFormatter.stringFromDate(dateOfBirth.date)
    var webServiceData: String = "username=\(userNameTextField.text)&password=\(passwordTextField.text)&name=\(nameTextField.text)&dob=\(strDate)"
    var concat: String = self.webservicelogin + webServiceData
    let url = NSURL(string: "www.samplepage.com")
    let session = NSURLSession.sharedSession()
    let dataTask = session.dataTaskWithURL(url!, completionHandler: { (data: NSData!, response:NSURLResponse!, error: NSError!) -> Void in
        //do something
        println("hi")
        println(NSString(data: data, encoding: NSUTF8StringEncoding))
    })
}

Upvotes: 0

Views: 1031

Answers (1)

Tom Elliott
Tom Elliott

Reputation: 1926

I believe this is a change introduced in Swift 2. Have you tried changing the let statement to:

let dataTask = session.dataTaskWithURL(url!, completionHandler: { (data: NSData?, response:NSURLResponse?, error: NSError?) -> Void in

I had some issues like this with a similar block and in a pinch, if you delete the completion handler and start over, autocomplete should point you in the right direction.

Upvotes: 2

Related Questions