swift 的if语句,switch语句,元组,函数

前端之家收集整理的这篇文章主要介绍了swift 的if语句,switch语句,元组,函数前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

swift if语句,switch语句,元组,函数


  1. //
  2. // main.swift
  3. // L00flow
  4. //
  5. // Created by hins on 16/10/26.
  6. // Copyright © 2016年 hins. All rights reserved.
  7. //
  8.  
  9. import Foundation
  10.  
  11. print("Hello,World!")
  12.  
  13. var str = "Hello";
  14.  
  15. for(var i=0;i<5;i=i+1){
  16. print(i)
  17. }
  18.  
  19. //流程控制
  20. //=======if语句
  21. for i in 1...10{
  22. if(i%2==0){
  23. print(i);
  24. }
  25. if(i != 3){
  26. print("ccccc")
  27. }
  28. if(i>5 && i<=7){
  29. print("jjjj")
  30. }else{
  31. print("ttttt")
  32. }
  33. }
  34.  
  35. //========switch语句
  36. var index = 3;
  37. switch (index){
  38. case 1 :
  39. print(index);
  40. break;
  41. case 2 :
  42. print(index);
  43. break;
  44. case 3 :
  45. print(index);
  46. break;
  47. default:
  48. break;
  49. }
  50.  
  51. //=============switch语句
  52. var valChar = "bc";
  53. switch (valChar){
  54. case "a" :
  55. print(valChar);
  56. break;
  57. case "b" :
  58. print(valChar);
  59. break;
  60. case "bc" :
  61. print(valChar);
  62. break;
  63. default:
  64. break;
  65. }
  66.  
  67.  
  68. //==========元组(区间)
  69. let somePoint = (1,1)
  70. switch somePoint {
  71. case (0,0):
  72. print("(0,0) is at the origin")
  73. case (_,0):
  74. print("(\(somePoint.0),0) is on the x-axis")
  75. case (0,_):
  76. print("(0,\(somePoint.1)) is on the y-axis")
  77. case (-2...2,-2...2):
  78. print("(\(somePoint.0),\(somePoint.1)) is inside the Box")
  79. default:
  80. print("(\(somePoint.0),\(somePoint.1)) is outside of the Box")
  81. }
  82. // prints "(1,1) is inside the Box"
  83.  
  84.  
  85. //============函数
  86. //无返回值
  87. func getString(name:String){
  88. print("wwwwww,\(name)")
  89. }
  90.  
  91. //调用
  92. getString("xuan");
  93.  
  94. //有返回值
  95. func getValueBB(name:String,age:Int) -> String {
  96. print(name);
  97. return name;
  98. }
  99.  
  100. //调用 (有些参数提示要带标签 如:age:23,否则报错)
  101. var a2 = getValueBB("nununu",age:23);
  102. print(a2)
  103.  
  104.  
  105. func getNums() -> (Int,Int){
  106. print("hello");
  107. return (2,3);
  108. }
  109.  
  110. //调用
  111. let (a,b) = getNums();
  112. print(b);
  113.  
  114. var fun22 = getValueBB;//函数赋值给变量
  115.  
  116. _ = fun22("test",age:45);//_ 下划线表示不接收返回值
  117.  
  118. //函数闭包 (函数中包含函数
  119.  
  120. func namefunc(){
  121. print("函数里面有一个函数");
  122. func namefunc333(){
  123. print("hahaha")
  124. }
  125. }

猜你在找的Swift相关文章