Result
Result type
throwingκ³Όμ λΉκ΅
// 1λ¨κ³: μλ¬νμ
μ μ
enum HeightError: Error {
case maxHeight
case minHeight
}
// 2λ¨κ³: throwing ν¨μ μ μ
func checkingHeight(height: Int) -> throws Bool {
if height > 190 {
throw HeightError.maxHeight
else if height < 130 {
throw HeightError.minHeight
} else {
if height >= 160 {
return true
} else {
return false
}
}
}
// 3λ¨κ³: μλ¬μ²λ¦¬
do {
let _ = try checkingHeight(height: 200)
print("κ°λ₯")
} catch {
print("λΆκ°λ₯"
}
// 2λ¨κ³: Result type ν¨μ μ μ
func CheckingHeight(height: Int) -> Result<Bool, HeightError> {
if height > 190 {
return Result.failure(HeightError.maxHeight)
} else if height < 130 {
return Result.failure(HeightError.minHeight)
} else {
if height >= 160 {
return Result.success(true)
} else {
return Result.success(false)
}
}
}
// 3λ¨κ³: μλ¬μ²λ¦¬
let result = CheckingHeight(height: 200)
switch result {
case .success(let data):
print("κ°λ₯")
case .failure(let error):
print("λΆκ°λ₯")
}Result Typeμ νμ©
Result νμ
μ μ¬μ©νλ μ΄μ λ?
Networkμμ Result νμ
μ νμ©
Last updated