swiftalamofirealamofire-request

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


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


Solution

  • 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.