Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot invoke 'dataTaskWithRequest' with an argument list of type '(NSMutableURLRequest, (_, _, _) throws -> _)'

Tags:

xcode

ios

swift

After start using Swift 2 in Xcode 7 Beta, I get an error cannot invoke. What cause this issue?

I try to figure out my problem by following these 2 questions, but i still get the error: Question 1, Question 2

Error:

Cannot invoke 'dataTaskWithRequest' with an argument list of type '(NSMutableURLRequest, (_, _, _) throws -> _)'

Complete Code

let task = NSURLSession.sharedSession().dataTaskWithRequest(request) {data, response, error in
            let json = try NSJSONSerialization.JSONObjectWithData(data, options: .MutableContainers) as? NSDictionary

            if let parseJSON = json {
                let resultValue:String = parseJSON["status"] as! String

                if(resultValue=="Success"){

                    //Store Confimed Account Detail Inside Core Data
                    try self.saveAccountDetail(userloginTextField!, confirmDataRetrieve: 0)

                    //Login is Successful
                    NSUserDefaults.standardUserDefaults().setBool(true, forKey: "isUserLoggedIn")
                    NSUserDefaults.standardUserDefaults().synchronize()

                    self.dismissViewControllerAnimated(true, completion: nil)

                }
            }

        }
like image 635
Brian Nezhad Avatar asked Jun 09 '15 16:06

Brian Nezhad


2 Answers

Thanks to Leo Dabus, with his help I figure out that this is the new feature in Swift 2. the way you type in the code should be to try or try! Handling

NSJSONSerialization should be run: (if is throwing input your remove the !)

let json = try!NSJSONSerialization.JSONObjectWithData(data!, options: .MutableContainers) as? NSDictionary
like image 169
Brian Nezhad Avatar answered Sep 29 '22 18:09

Brian Nezhad


Change from try to try! Then it will start compiling as norm.

like image 27
Jignesh Avatar answered Sep 29 '22 18:09

Jignesh