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)
}
}
}
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
Change from try
to try!
Then it will start compiling as norm.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With