Swift 2 try/catch - ios

I've started converting one of my projects to Swift 2 and I ran into this issue. To start this block below is perfectly valid try/catch, in fact it was generated by the Xcode migration tool.
do {
requestData = try NSJSONSerialization.dataWithJSONObject(requestContents, options: [])
} catch var error as NSError {
requestError = error
requestData = nil
}
If I use that same code inside a closure, such as a dataTaskWithRequest I get an error. The error is at the task assignment, but its the catch that causes it. The following also works but I'm not capturing the error.
let task = session.dataTaskWithRequest(request, completionHandler: { (data, response, taskError) -> Void in
if taskError != nil {
NSLog("Error making request: " + taskError!.localizedDescription)
}
else {
do {
let json = try NSJSONSerialization.JSONObjectWithData(data!, options: .MutableLeaves) as? NSDictionary
if let parseJSON = json as NSDictionary? {
// do some parsing here
}
}
catch {
NSLog("Error in JSON serialization")
}
}
})
task.resume()
but if I try to capture the error with:
} catch let e as NSError {
I get this error:
Invalid conversion from throwing function of type '(_, _, _) throws ->
Void' to non-throwing function type '(NSData?, NSURLResponse?,
NSError?) -> Void'
I did discover that:
} catch _ {
works but a lot of good that does me.
Am I missing something or should I be filing a bug?
(This is Xcode 7b5)

The completion handler of dataTaskWithRequest is not designed to throw error like JSONObjectWithData of NSJSONSerialization, whose signature is:
class func JSONObjectWithData(data: NSData, options opt: NSJSONReadingOptions) throws -> AnyObject
Doing the following would work (same as what you have tried):
catch _
But that won't give us any detail about the error from the one that throws, namely, from class func JSONObjectWithData.
As a result, we need a way to consume the non-throwable dataTaskWithRequest while preserving the one that throws - JSONObjectWithData.
I have tried the following:
catch let error as NSError
{
//error specific to JSON serialization
NSLog("Error in JSON serialization \(error)")
}
catch
{
//exhaust the error
NSLog("Some error")
}

Related

Migration Alamofire 4 to 5 build issue

I am doing migration after 2 years a lots things have been changed, now flagging a lots of error while building. Most are related to Alamofire 5. Now there are many error keeps coming fixing one by one.
Error: // ERROR: Cannot specialize non-generic type
public static func ObjMappingSerializer<T: Mappable>(_ keyPath: String?) -> DataResponseSerializer<T> { 'DataResponseSerializer'
return DataResponseSerializer { request, response, data, error in
//LogResponse(response, data: data, error: error)
Logger._reqresLogger.logResponse(response, data: data, error: error)
guard error == nil else {
return .failure(parseErrorResponse(data: data, response: response, errorType: error!))
}
guard let _ = data else {
return .failure(errorForNilData())
}
let JSONToMap = deserializeJSON(request: request, response: response, data: data, error: error, keyPath: keyPath)
if let json = JSONToMap as? [String:Any], let parsedObject = Mapper<T>().map(JSON:json) {
return .success(parsedObject)
}
let errorCode = response?.statusCode ?? NSURLErrorCannotParseResponse
return .failure(APIError(code: errorCode, errorUserInfo: nil))
}
}
Fixed by autosuggestion however next error comes
Error: Trailing closure passed to parameter of type 'DataPreprocessor' that does not accept a closure
public static func ObjMappingSerializer(_ keyPath: String?) -> DataResponseSerializer {
return DataResponseSerializer { request, response, data, error in
//LogResponse(response, data: data, error: error)
Logger._reqresLogger.logResponse(response, data: data, error: error)
guard error == nil else {
return .failure(parseErrorResponse(data: data, response: response, errorType: error!))
}
guard let _ = data else {
return .failure(errorForNilData())
}
let JSONToMap = deserializeJSON(request: request, response: response, data: data, error: error, keyPath: keyPath)
if let json = JSONToMap as? [String:Any], let parsedObject = Mapper<T>().map(JSON:json) {
return .success(parsedObject)
}
let errorCode = response?.statusCode ?? NSURLErrorCannotParseResponse
return .failure(APIError(code: errorCode, errorUserInfo: nil))
}
}
Now in Alamofire many methods have been removed in Alamofire 5. How can I fix these errors?
You can no longer initialize a DataResponseSerializer with a closure. I suggest you reevaluate your parsing needs and rebuild around responseDecodable. If you need, you can create your own serializer by adopting ResponseSerializer. Your logic would be the same, just copied into the parse method.

Invalid conversion from throwing function of type '(_, _) throws -> ()' to non-throwing function type '(JSON?, Error?) -> Void'

I am having an error of
EduappRestClient.request(with: URLString, method: .post, parameters: parameters) { (json, error) in
guard error == nil, let json = json else {
completion(nil, error)
return
}
let result = try JSONDecoder().decode(QuestionModel.self, from: json)
completion(result, nil)
}
it's an API i am calling and my full source code can be found at
https://github.com/WilliamLoke/quizApp
may i know what is the issue i am getting this line of error code?
Since this block is not expected to throw an error, you need to wrap your throwing call in a do catch block:
EduappRestClient.request(with: URLString, method: .post, parameters: parameters) { (json, error) in
guard error == nil, let json = json else {
completion(nil, error)
return
}
do {
let result = try JSONDecoder().decode(QuestionModel.self, from: json)
completion(result, nil)
} catch let error {
completion(nil, error)
}
}
I've got the same problem, and the solution is easy: you can just use try? instead of try
guard let result = try? JSONDecoder().decode(QuestionModel.self, from: json)

Call can throw, but it is not marked with 'try' and the error is not handled Swift 4, SwiftyJSON [duplicate]

I am trying to use swiftyjson and I am getting an Error:
Call can throw, but it is marked with 'try' and the error is not
handled.
I have validated that my source JSON is good. I've been searching and cannot find a solution to this problem
import Foundation
class lenderDetails
{
func loadLender()
{
let lenders = ""
let url = URL(string: lenders)!
let session = URLSession.shared.dataTask(with: url)
{
(data, response, error) in
guard let data = data else
{
print ("data was nil?")
return
}
let json = JSON(data: data)
print(json)
}
session.resume()
}
}
Thank you for all the help!
The SwiftyJSON initializer throws, the declaration is
public init(data: Data, options opt: JSONSerialization.ReadingOptions = []) throws
You have three options:
Use a do - catch block and handle the error (the recommended one).
do {
let json = try JSON(data: data)
print(json)
} catch {
print(error)
// or display a dialog
}
Ignore the error and optional bind the result (useful if the error does not matter).
if let json = try? JSON(data: data) {
print(json)
}
Force unwrap the result
let json = try! JSON(data: data)
print(json)
Use this option only if it's guaranteed that the attempt will never fail (not in this case!). Try! can be used for example in FileManager if a directory is one of the default directories the framework creates anyway.
For more information please read Swift Language Guide - Error Handling
You should wrap it into a do-catch block. In your case:
do {
let session = URLSession.shared.dataTask(with: url) {
(data, response, error) in
guard let data = data else {
print ("data was nil?")
return
}
let json = JSON(data: data)
print(json)
}
} catch let error as NSError {
// error
}
Probably you need to implement do{} catch{} block. Inside do block you have to call throwable function with try.

Swift: Extra argument 'error' in call

I'm currently developing my first iOS app using Swift 2.0 and Xcode Beta 2. It reads an external JSON and generates a list in a table view with the data. However, I'm getting a strange little error that I can't seem to fix:
Extra argument 'error' in call
Here is a snippet of my code:
let task = session.dataTaskWithURL(url!, completionHandler: {data, response, error -> Void in
print("Task completed")
if(error != nil){
print(error!.localizedDescription)
}
var err: NSError?
if let jsonResult = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: &err) as? NSDictionary{
if(err != nil){
print("JSON Error \(err!.localizedDescription)")
}
if let results: NSArray = jsonResult["results"] as? NSArray{
dispatch_async(dispatch_get_main_queue(), {
self.tableData = results
self.appsTableView!.reloadData()
})
}
}
})
The error is thrown at this line:
if let jsonResult = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: &err) as? NSDictionary{
Can someone please tell me what I'm doing wrong here?
With Swift 2, the signature for NSJSONSerialization has changed, to conform to the new error handling system.
Here's an example of how to use it:
do {
if let jsonResult = try NSJSONSerialization.JSONObjectWithData(data, options: []) as? NSDictionary {
print(jsonResult)
}
} catch let error as NSError {
print(error.localizedDescription)
}
With Swift 3, the name of NSJSONSerialization and its methods have changed, according to the Swift API Design Guidelines.
Here's the same example:
do {
if let jsonResult = try JSONSerialization.jsonObject(with: data, options: []) as? [String:AnyObject] {
print(jsonResult)
}
} catch let error as NSError {
print(error.localizedDescription)
}
Things have changed in Swift 2, methods that accepted an error parameter were transformed into methods that throw that error instead of returning it via an inout parameter. By looking at the Apple documentation:
HANDLING ERRORS IN SWIFT:
In Swift, this method returns a nonoptional result and is marked with the throws keyword to indicate that it throws an error in cases of failure.
You call this method in a try expression and handle any errors in the catch clauses of a do statement, as described in Error Handling in The Swift Programming Language (Swift 2.1) and Error Handling in Using Swift with Cocoa and Objective-C (Swift 2.1).
The shortest solution would be to use try? which returns nil if an error occurs:
let message = try? NSJSONSerialization.JSONObjectWithData(receivedData, options:.AllowFragments)
if let dict = message as? NSDictionary {
// ... process the data
}
If you're also interested into the error, you can use a do/catch:
do {
let message = try NSJSONSerialization.JSONObjectWithData(receivedData, options:.AllowFragments)
if let dict = message as? NSDictionary {
// ... process the data
}
} catch let error as NSError {
print("An error occurred: \(error)")
}
This has been changed in Swift 3.0.
do{
if let responseObj = try JSONSerialization.jsonObject(with: results, options: .allowFragments) as? NSDictionary{
if JSONSerialization.isValidJSONObject(responseObj){
//Do your stuff here
}
else{
//Handle error
}
}
else{
//Do your stuff here
}
}
catch let error as NSError {
print("An error occurred: \(error)") }

Compiler error in custom Alamofire response function

I'm attempting to implement a generic responseObject function for Alamofire using Argo. Unfortunately I'm getting a persistent compiler error:
error: missing argument for parameter #2 in call
APIManager.manager.request(APIRouter.Login(username: usernameTextField.text, password: passwordTextField.text)).responseObject { (object, error) -> Void in
I understand this error to usually mean something about tuples, so I'm guessing something in my code is being misinterpreted as a tuple, but I can't figure out what. Here's my responseObject function:
func responseObject<T: Decodable where T == T.DecodedType>(completionHandler: (T?, NSError?) -> Void) -> Self {
return responseJSON(options: .AllowFragments) { (request, response, JSON, error) in
// TODO: Complete error handling.
var responseObject: T?
if let JSON: AnyObject = JSON,
let response: Response = decode(JSON) where response.status == "0" {
responseObject = decode(JSON)
}
completionHandler(responseObject, error)
}
}
My request code and router work fine, it's the addition of this responseObject function that breaks things.
Turns out you need to provide an explicit type to the variables brought into a closure with a generic return type. So instead of (object, error) in I needed (object: SpecificType?, error).

Resources