if case tests a value against one pattern and, if it matches, lets you bind parts of it: if case .success(let user) = result { ... }.
A switch must cover every case. When you only care about one, if case is shorter, and it can bind associated values.
enum Response {
case success(User)
case failure(Error)
case loading
}
if case .success(let user) = response {
print(user.name)
}
// Combine with a condition
if case .success(let user) = response, user.isActive { }
// Ranges and optionals work too
if case 200...299 = statusCode { }
if case let value? = optionalValue { }
// Exit early
guard case .success(let user) = response else { return }
// Loop over matching elements only
for case .success(let user) in responses { print(user.name) }Note that the pattern is on the left and the value on the right, which is the opposite of a normal comparison. For simple enums without associated values, == is usually clearer.
