发布于 2017-06-19 06:17:30 | 171 次阅读 | 评论: 0 | 来源: 网友投递

这里有新鲜出炉的Swift教程,程序狗速度看过来!

Swift编程语言

SWIFT,苹果于2014年WWDC(苹果开发者大会)发布的新开发语言,可与Objective-C*共同运行于Mac OS和iOS平台,用于搭建基于苹果平台的应用程序。


这篇文章主要介绍了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")
}

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



最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务