Nowaday, there are a lot of solution to handle response which has multiple states.
func getUserDetail(completion: (User?, NSError?) -> Void) {...}
or
func getUserDetail(success: User -> Void, failure: NSError -> Void) {...}
I hate this, when ever I call that method I have to check error != nil
getUserDetail { user, error in
if let error = error {
//Do when failed
} else if let user = user {
//Do when success
} else {
//What should I do here
}
}
or kind of prolix :) but simple right
getUserDetail({ user in
//Do when success
}, failure: { error in
//Do when failed
})
OK now I will do some think more complicated, but I think good for me (not recomended for everybody)
First, create an enum Response:
enum Response<Value> {
case success(Value)
case failure(NSError)
}
Time to for enum
:
func getUserDetail(completion: Response<User> -> Void) {
// if success
completion(.success(user))
//..
// if failed
completion(.failure(error))
}
Call your method:
getUserDetail { response in
switch response {
case .Success(let user):
//Do when success
case .Failure(let error):
//Do when failed
}
}
Letβs swifty! πππ