有没有办法合并switch
下面的语句以包含可选绑定?
一点背景背景...
首先这是我的结果类型:
enum Result<Value> {
case success(Value)
case failure(Swift.Error)
}
Facebook SDK 的一个扩展:
func start(_ completionHandler: @escaping (Result<Any>) -> Void) -> FBSDKGraphRequestConnection
{
return start() { (_, response, error) in
switch (response, error) {
case (.some(let result), .none):
completionHandler(Result(value: result))
case (.none, .some(let error)):
completionHandler(Result(error: error))
case (.none, .none), (.some, .some):
preconditionFailure("Unexpected State")
}
}
}
最后,我要重构的那段代码。我的目标是避免重复self.present(error:)
代码:
_ = FBSDKGraphRequest.emailRequest().start() { (result: Result<Any>) in
switch result {
case .success (let value as [ String : Any ]):
...
if let email = value["email"] as? String {
...
}
else {
self.present(error: Error(.facebookOther))
}
default:
self.present(error: Error(.facebookOther))
}
}