swift guard关键字详解及使用
Swift提供guard关键字,guard关键字可以简化繁琐的判断逻辑
func buy( money: Int , price: Int , capacity: Int , volume: Int){
if money >= price{
if capacity >= volume{
print("I can buy it!")
print("(money-price) Yuan left.")
print("(capacity-volume) cubic meters left")
}
else{
print("No enough capacity")
}
}
else{
print("Not enough money")
}
}
以上代码用guard关键字简化代码风格
func buy2( money: Int , price: Int , capacity: Int , volume: Int){
guard money >= price else{
print("Not enough money")
return
}
guard capacity >= volume else{
print("Not enough capacity")
return
}
print("(money-price) Yuan left.")
print("(capacity-volume) cubic meters left")
}
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
最后
以上就是酷酷大树最近收集整理的关于swift guard关键字详解及使用的全部内容,更多相关swift内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复