Created
February 17, 2020 05:02
-
-
Save yimajo/fbca63163b6e5c9dac9e534dbc4723b7 to your computer and use it in GitHub Desktop.
Swift5.2のcallAsFunctionを使ってUseCaseの必須なメソッドとしてみる
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
protocol UseCase where Failure: Error { | |
associatedtype Parameters | |
associatedtype Success | |
associatedtype Failure | |
func callAsFunction(_ parameters: Parameters, completion: ((Result<Success, Failure>) -> ())?) | |
} | |
struct User { | |
let id: UUID | |
} | |
class GetUserInteractor: UseCase { | |
func callAsFunction(_ parameters: UUID, completion: ((Result<User, Never>) -> ())?) { | |
// 本来ならなんやかんや色々あるけど今回はとりあえず返すだけ | |
completion?(.success(User(id: parameters))) | |
} | |
} | |
let getUser = GetUserInteractor() | |
getUser(UUID()) { | |
switch $0 { | |
case .success(let user): | |
print(user) | |
case .failure: | |
break | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment