swift 基础笔记七(for循环)

前端之家收集整理的这篇文章主要介绍了swift 基础笔记七(for循环)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
  1. //: Playground - noun: a place where people can play
  2.  
  3. import UIKit
  4.  
  5. // for 循环
  6.  
  7. //你可以使用for-in循环来遍历一个集合里面的所有元素,例如由数字表示的区间、数组中的元素、字符串中的字符。
  8.  
  9.  
  10. for index in 1...5{
  11. println(index);
  12. }
  13.  
  14. //如果你不需要知道区间内每一项的值,你可以使用下划线(_)替代变量名来忽略对值的访问
  15. var count = 3;
  16. var one = 1;
  17. var two = 2;
  18. for _ in 1...count{
  19. one *= two
  20. }
  21.  
  22. // 遍历数组
  23.  
  24. var arr1 = ["E","n","d","a"];
  25. for name in arr1{
  26. println(name);
  27. }
  28.  
  29. // key + value
  30. var arr2 = [1:"E",2:"n",3:"d",4:"a"];
  31. for (key,value) in arr2{
  32. println("\(key)==>\(value)")
  33. }
  34.  
  35.  
  36. // 遍历字符串
  37.  
  38. var names = "MyNameisEnda";
  39.  
  40. for name in names{
  41. println("\(name)");
  42. }
  43.  
  44. // for 条件递增
  45. for var index = 0;index<3;++index{
  46. println(index);
  47. }

猜你在找的Swift相关文章