It is an async call so you can use completion
to do this. And you can pass Result
type in completion with values
func gettingFunction(name: String, _ completion: @escaping (Result<Int, Error>) -> Void {
Coinpaprika.API.coins().perform { response in
switch response {
case .success(let coins):
completion(.success(coins[0].id))
case .failure(let error):
print(error)
completion(.failure(error))
}
}
}
Usage
gettingFunction(name: "asd") { result in
switch result {
case .success(let id):
print("ID is:", id)
case .failure(let error):
print("Error is:", error)
}
}
If you want to use the value in another function. You should call this function in .success
case because we have the ID value in this case.
gettingFunction(name: "asd") { result in
switch result {
case .success(let id):
print("ID is:", id)
anotherFunction(id: id)
case .failure(let error):
print("Error is:", error)
}
}