swift-学习之路,字典、数组、循环-控制流

前端之家收集整理的这篇文章主要介绍了swift-学习之路,字典、数组、循环-控制流前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
  1. //
  2. // ViewController.swift
  3. // UIWindow
  4.  
  5.  
  6. import UIKit
  7. import Foundation
  8. class ViewController: UIViewController {
  9.  
  10. @IBOutlet weak var usrNameLabel: UITextField!
  11. @IBOutlet weak var passwdLabel: UITextField!
  12.  
  13. @IBAction func btnTouch(sender: AnyObject) {
  14. println("usrName is \(self.usrNameLabel.text)");
  15. println("passwdLabel is \(self.passwdLabel.text)");
  16. }
  17. override func viewDidLoad() {
  18. super.viewDidLoad()
  19. println("Hello World");
  20. let label = "The width is"
  21. let width = 90
  22. let widthLabel = label + String(width)
  23. println("widthLabel is \(widthLabel)")
  24. var shoppingList = ["catfish","water","tulips","blue paint"]
  25. shoppingList[1] = "bottle of water"
  26. var occupations = [
  27. "Malcolm":"Captain","Kaylee":"Machanic",]
  28. occupations["Jayne"] = "Public Relation"
  29. //初始化一个空的数组
  30. let emptyArray = [String]()
  31. //初始化一个空的字典
  32. let emptyDictionary = [String : Float]()
  33. let individualscores = [75,43,103,87,12]
  34. var teamscore = 0
  35. //循环语句的使用
  36. for score in individualscores{
  37. if(score > 50)
  38. {
  39. teamscore += 3
  40. }
  41. else
  42. {
  43. teamscore += 1
  44. }
  45. }
  46. print(teamscore)
  47. var optionalString:String? = "Hello"
  48. optionalString == nil
  49. var optionalName:String? = "John Appleasee"
  50. var greeting = "Hello"
  51. /*if let name = optionalName{
  52. greeting = "Hello,\(name)"
  53. }
  54. */
  55. //如果可选值为nil 则不会执行代码块的内容
  56. /*
  57. optionalName = nil
  58. if let name = optionalName{
  59. greeting = "Hello \(name)"
  60. }
  61. print(greeting)
  62. */
  63. optionalName = nil
  64. if let name = optionalName{
  65. greeting = "Hello \(name)"
  66. }else{
  67. greeting = "-执行的第二句代码-"
  68. }
  69. print(greeting)
  70. /**
  71. switch 支持任意类型的数据以及各种比较操作-不限于整数和表达式
  72. // 不需要加break
  73. */
  74. let vegetable = "red pepper"
  75. switch vegetable{
  76. case "celery":
  77. let vegetableComment = "Add some raisins and make ants on a log."
  78. case "cucumber","watercress":
  79. let vegetableComment = "That would make a good tea sandwich."
  80. case let x where x.hasSuffix("pepper"):
  81. let vegetableComment = "Is it a spicy\(x)?"
  82. default:
  83. let vegetableComment = "Everything tastes good in soup."
  84. }
  85. /// 这里用于比较字典中的最大值并且把变量是哪种类型的输出
  86. let interestingNumbers = [
  87. "Prime":[2,3,5,7,11,13],"Fibonacci":[1,1,2,8],"Square":[1,4,9,16,25],]
  88. var largest = 0
  89. var name:String?
  90. for (kind,numbers) in interestingNumbers{
  91. for number in numbers{
  92. if number > largest{
  93. largest = number
  94. name = kind
  95. }
  96. }
  97. }
  98. println(largest)
  99. println(name)
  100. /**
  101. 1+2+....+100;
  102. */
  103. //这里用起来十分的方便,程序不运行会有提示,可以试试如果i的值不++ 可以观察一下
  104. var sum = 0
  105. var i = 0
  106. while i <= 100{
  107. sum += i
  108. i++
  109. }
  110. print(sum)
  111. /**
  112. ..< 和 ... 表示范围
  113. ..< 不包含上届 ...包含上届
  114. */
  115. var firstForLoop = 0
  116. for i in 0..<3{
  117. firstForLoop += i
  118. }
  119. print(firstForLoop)
  120. var secondForLoop = 0
  121. for var i = 0;i < 3; ++i{
  122. secondForLoop += i;
  123. }
  124. print(secondForLoop)
  125. //self.usrNameLabel.
  126. // Do any additional setup after loading the view,typically from a nib.
  127. }
  128.  
  129. override func didReceiveMemoryWarning() {
  130. super.didReceiveMemoryWarning()
  131. // Dispose of any resources that can be recreated.
  132. }
  133.  
  134.  
  135. }

猜你在找的Swift相关文章