Reputation: 3100
I migrated my app to Swift 2 and I get the error
Invalid conversion from throwing function of type
on the following line:
NSURLConnection.sendAsynchronousRequest(urlRequest, queue: NSOperationQueue.mainQueue(), completionHandler: {
Here's the full code block:
NSURLConnection.sendAsynchronousRequest(urlRequest, queue: NSOperationQueue.mainQueue(), completionHandler: {
response, data, error in
let image = UIImage(data: data!)
self.profilePic.image = image
if var realUser = user {
realUser["image"] = data as! AnyObject
try realUser.save()
FBRequestConnection.startForMeWithCompletionHandler({
connection, result, error in
realUser["first_name"] = result["first_name"]
realUser["last_name"] = result["last_name"]
try realUser.save()
})
}
})
How do I fix this code with the new catch implemented in Swift 2?
Thanks!
Upvotes: 1
Views: 577
Reputation: 5741
Try this code:
NSURLConnection.sendAsynchronousRequest(urlRequest, queue: NSOperationQueue.mainQueue(), completionHandler: {
response, data in
let image = UIImage(data: data!)
self.profilePic.image = image
if var realUser = user {
realUser["image"] = data as! AnyObject
do {
try realUser.save()
}
catch {
print(error)
}
FBRequestConnection.startForMeWithCompletionHandler({
connection, result, error in
realUser["first_name"] = result["first_name"]
realUser["last_name"] = result["last_name"]
do {
try realUser.save()
}
catch {
print(error)
}
})
}
})
Upvotes: 0
Reputation: 118691
The block passed to sendAsynchronousRequest
is not allowed to throw errors. So you have to catch the errors:
do {
try realUser.save()
}
catch {
// handle the error
}
Upvotes: 2