νμ¬ μ€νμΌ κ°μ΄λλ μ€μ²© if
guard
μ¬μ©μ λν΄ λͺ
ννμ§λ§ guard
λμ if
λ₯Ό μ¬μ©ν΄μΌ νλ μλ₯Ό 보μ¬μ€ μ μμ΅λλ€. νμν μ΅μ
μ λν ν΄μ κ° μλ κ²½μ°:
if !success { return false }
guard success else { return false }
μ¬κΈ°μ κ°μ₯ μ μ€λͺ λμ΄ μμ΅λλ€: https://www.natashatherobot.com/swift-when-to-use-guard-vs-if/
λλ Failing Guards μΉμ
μ΄ μ΄κ²μ λ€λ£¨κ³ μλ€κ³ κ±°μ λ§ν κ²μ
λλ€.
Guard statements are required to exit in some way.
...λλ λνμ€μ λΈλ‘κ·Έμμ λ€μκ³Ό κ°μ΄ λ§νμ΅λλ€.
think of guard as a lightweight Assert
λ°λΌμ λ€μμ μνν΄μΌ ν©λλ€.
guard success else { return false }
μ¬κΈ°μ guard
μ μ΄μ μ μ»΄νμΌλ¬κ° μ’
λ£λ₯Ό 보μ₯νλ€λ κ²μ
λλ€. μμ:
// This compiles fine and a bug risks going unnoticed
if !success {
// Some code here
// Forgot the return statement
}
// Compiler error, you are required to return and the bug is prevented
guard success else {
// Some code here
// Forgot the return statement
}
@RobertGummesson λ°λΌμ μ ν μ¬νμ νμ§ μμλ λλ κ²½μ°μλ μ’
λ£ν΄μΌ νλ μ€λ₯κ° λ°μνλ©΄ guard
λ³΄λ€ if
λ₯Ό μ¬μ©νλ κ²μ΄ μ’μ΅λλ€. μ³μ?
@agirault - μ, λλ λ¨μν μ’ λ£κ° νμν©λλ€(μ€ν¨λ‘ μΈν κ²μΈμ§ μ¬λΆμ κ΄κ³μμ΄).
κ°μ₯ μ μ©ν λκΈ
λλ Failing Guards μΉμ μ΄ μ΄κ²μ λ€λ£¨κ³ μλ€κ³ κ±°μ λ§ν κ²μ λλ€.
Guard statements are required to exit in some way.
...λλ λνμ€μ λΈλ‘κ·Έμμ λ€μκ³Ό κ°μ΄ λ§νμ΅λλ€.
think of guard as a lightweight Assert
λ°λΌμ λ€μμ μνν΄μΌ ν©λλ€.
μ¬κΈ°μ
guard
μ μ΄μ μ μ»΄νμΌλ¬κ° μ’ λ£λ₯Ό 보μ₯νλ€λ κ²μ λλ€. μμ: