这篇文章主要介绍了swift guard关键字详解及使用的相关资料,需要的朋友可以参考下
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关键字详解及使用
基础教程推荐
猜你喜欢
- go语言的魔幻旅程14-反射 2023-09-05
- R语言使用gganimate创建可视化动图 2022-12-10
- R语言多元线性回归实例详解 2022-12-15
- R语言histogram(直方图)的具体使用 2022-10-28
- ruby-on-rails – Nginx支持的Rails应用程序中缺少Content-Length Header 2023-09-20
- Ruby on Rails在Ping ++ 平台实现支付 2023-07-22
- R语言关联规则深入详解 2022-11-08
- Go语言实现一个Http Server框架(二) Server的抽象 2023-07-25
- R语言学习代码格式一键美化 2022-12-05
- golang 自然语言处理工具(gohanlp) 2023-09-05
