源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

swift guard关键字详解及使用

  • 时间:2020-04-17 15:43 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:swift guard关键字详解及使用
[b]swift guard关键字详解及使用[/b] [b]Swift提供guard关键字,guard关键字可以简化繁琐的判断逻辑[/b]
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")
}

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部