How to fix Invalid conversion from throwing function of type X to non-throwing function type Y with Do-Try-Catch
The do-catch
is currently outside of the closure that has the try
statement in it.
You'll need to move the do-catch
into the closure. The error is because weather
is expecting a closure that doesn't throw but it's getting on that does.
Invalid conversion from throwing function of type '(_, _) throws - ()' to non-throwing function type '(Bool, Error?) - Void
You cannot throw from within the non throwing completion handler. It is asynchronous! Just as you cannot return a value from within an asynchronous function, so too you cannot throw there. After all, to catch the throw, the throw would need to travel backwards in time to when the original function was called. And that is metaphysically impossible.
Instead you must provide another completion handler such that you can call it with an Error parameter from within the non throwing completion handler. That is exactly why the Result type was invented, to allow you to propagate an error through an asynchronous situation.
Invalid conversion from throwing function of type '(_) throws - ()' to non-throwing function type '(Response) - Void'
You cannot
throw ValidationError.athenticationFailure
because the request is asynchronous. What you can do is to change the completion type to Result<ResponseModel<SignUpModel>, ValidationError>
to return
completion(.success(responseModel))
on success and
completion(.failure(athenticationFailure)
on failure. By the way I buy an u
Invalid conversion from throwing function to non-throwing function , trying to store a closure and it can't throw
Your throws
is in the wrong place:
static func markdownReader() throws -> Self {
markdownReader()
cannot throw. It just returns a Reader. The throwing, if any is going to happen, will happen in try file.readAsString
, which is inside the closure. The throw does not magically somehow filter up out of that closure definition to the place where it is defined!
So basically you have two choices. One is to declare that the closure type can in fact throw:
var convert: (File) throws -> Page
The other is: don't let the throw percolate up out of the closure. Catch it:
do {
let contents = try file.readAsString(encodedAs: .utf8)
return Page(slug: "", title: "", content: contents, html: "")
} catch {
// ???
}
The problem then is that in the catch
block you are still obligated to return a Page (unless you'd like to fatalError
at this point, on the grounds that the file can't be read so we may as well die). So you'd have to make up a page with fake content
, since you failed to obtain the contents from the File.
Invalid conversion from throwing function of type '(_, _) throws - ()' to non-throwing function type '(JSON?, Error?) - Void'
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)
}
}
Invalid conversion from throwing function of type '(_) throws - ()' to non-throwing function type '(DataSnapshot) - Void'
You are throwing an error in the completion block. This is not possible and causes the error.
The return value of the closure is not related to the return value of the enclosing function – strictly spoken throws
is not a return value but is affected, too.
To be able to return something from the closure you have to implement a completion block rather than throws
func getUserList(completion : (Error?) -> ())
and use it
completion(value.isEmpty ? UserError.Empty : nil)
Side-note: You are using too many question and exclamation marks. Use optional binding to unwrap optionals for example (and use Swift native collection types)
if let value = snapshot.value as? [String:Any] {
for key in value.keys { ...
Alamofire 4 Invalid conversion from throwing function of type '(_) throws - ()' to non-throwing function type '(DataResponse Any ) - Void'
The error occurs because you don't handle the errors of the throw
ing functions.
Add a do - catch block
do {
let clear = try ClearMessage(string: keyAndiv, using: .utf8)
let encrypted = try clear.encrypted(with: publicKey, padding: .PKCS1)
let data = encrypted.data
let base64String = encrypted.base64Encoded
print ("data", data);
print ("base64String", base64String)
} catch { print(error) }
And this is Swift, you don't need parentheses around if
conditions and trailing semicolons
if response.result.value != nil { ...
or better
guard let result = response.result.value else { return }
let swiftyJSON = JSON(result)
Related Topics
Table View Cellforrowatindexpath Warning
Sharing File Data Between Applications in Swift/Ios
Can You Enforce a Typealias in Swift
Swift Spritekit I Detect a Collison But It Reads the Collision Mulitple Times
How to Return a Value from a Void Closure in Swift
Making Button Span Across VStack
How to Create a Directory in Downloads Folder with Swift on MACos? Permission Exception
How to Cast Generic Number Type 'T' to Cgfloat
Swift Generics: Non-Nominal Type Does Not Support Explicit Initialization
What's Wrong with My #If Target_Os_Simulator Code for Realm Path Definition
Swift. Is the (Absolutely) Sole Specific Advantage of Unowned Over Weak, Performance
How to Use Unsafemutablepointer<Opaquepointer> in Swift
Why Doesn't Swift Force My Designated Initializer to Call Super
How to Handle Parameter Validation Swift