Home > Net >  Conditional cast from 'AFError' to 'AFError' always succeeds
Conditional cast from 'AFError' to 'AFError' always succeeds

Time:12-18

I'm using Alamofire and after I do the fetch request I'm handling the error with:

guard case let .failure(error) = response.result else { return }

if let error = error as? AFError {
    switch error {
            ...
    }
}

Problem:

In line if let error = error as? AFError { Xcode has a warning:

Conditional cast from 'AFError' to 'AFError' always succeeds

How can I Fix (preferred) or silence that warning?

I tried:

Removing it like this:

if let error = error {

but it says:

Initializer for conditional binding must have Optional type, not 'AFError'

Thanks

CodePudding user response:

You’ve already handled the case where you don’t have an error by returning. Thus, error is guaranteed to be a valid error object - it is not an optional, so you don’t need if let at all.

  • Related