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