美文网首页
关于guard异常解决方案

关于guard异常解决方案

作者: suys_2017 | 来源:发表于2017-08-04 09:20 被阅读17次

写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异常解决方案

      本文链接:https://www.haomeiwen.com/subject/jqfvlxtx.html