关于guard异常解决方案

写swift的时候可以经常利用guard取消强拆包的,但是不要忘记不满足条件的时候需要一个返回路径

1.直接return掉

例如:

guard let sguard = sguard else { 

print ("There is no kitten")

return

}

2.利用枚举定义错误码,throw抛出异常

enum GuardError: ErrorType {

case GuardNormalError

}

guard let sguard = sguard else {

print ("There is no kitten")

throw GuardError.GuardNormalError

}

3.系统函数,调用一个@noreturn功能函数 fatalError( ) 解决方案

guard let sguard = sguard else {

print ("There is no kitten")

fatalError()

}

你可能感兴趣的:(关于guard异常解决方案)