Reputation: 6258
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)
}
}
}
Upvotes: 5
Views: 4581
Reputation: 236260
try like this:
let task = NSURLSession.sharedSession().dataTaskWithRequest(request,
completionHandler: {
(data, response, error) -> Void in
if let data = data {
println(data.length)
// you can use data here
} else if let error = error {
println(error.description)
}
})
task!.resume()
you can test with this one
let task = NSURLSession.sharedSession().dataTaskWithRequest(
NSURLRequest(URL: NSURL(string: "https://cdn.photographylife.com/wp-content/uploads/2014/06/Nikon-D810-Image-Sample-6.jpg")!),
completionHandler: {
(data, response, error) -> Void in
if let data = data {
println(data.length)
if let image = UIImage(data: data) {
println(image.description)
}
} else if let error = error {
println(error.description)
}
})
task!.resume()
Upvotes: 1
Reputation: 6258
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
Upvotes: 5