欢迎来到代码驿站!

当前位置:首页 >

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")
}

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

上一篇:Javascript中正则表达式的使用及基本语法

栏    目:

下一篇:docker 容器上编译 go 程序提示找不到文件问题

本文标题:swift guard关键字详解及使用

本文地址:http://www.codeinn.net/misctech/146694.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有