swift guard关键字详解及使用
时间:2021-06-23 09:20:31|栏目:|点击: 次
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")
}
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
栏 目:
下一篇:docker 容器上编译 go 程序提示找不到文件问题
本文标题:swift guard关键字详解及使用
本文地址:http://www.codeinn.net/misctech/146694.html






