前端之家收集整理的这篇文章主要介绍了
Swift 学习小结:简单值 及流程控制,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
- import UIKit
-
- var str = "Hello,playground"
- //简单值
- //let 声明常量 var 声明变量
- var value = 123;
- var i:Int = 0;
- var num = 0;
-
- var maVariable = 42;
- maVariable = 50;
- let myConstant = 42;
-
- let implicitInteger = 70
- let implicitDouble = 70.0
- let explicitDoublie:Double = 70
-
- let label = "This width is"
- let width = 94
- let widthLabel = label + String(width)
-
- let apples = 3
- let oranges = 5
- let applesSummary = "I have \(apples) apples"
- let orangesSummary = "I have \(oranges) oranges"
-
- var shoppingList = ["catfishi","water","tulips","blue paint"]
- shoppingList[1] = "bottle of water"
- var occupations = [
- "Malcolm":"Captain","Kaylee":"Mechanic"
- ]
- occupations["Jsyne"] = "Public Relations"
-
- let emptyAraay = String[]()
- let emptyDictionary = Dictionary<String,Float>()
-
- shoppingList = []
-
- //流程控制
- switch ("abc")
- {
- case "123":
- println("123");
- case "456","abc":
- println("123 abc")
- default:
- println("没有找到合适的匹配")
- }
- var i:Int = 0;
- while(i<10)
- {
- i++;
- println(i);
- }
- do
- {
- i--;
- println(i);
- }while (i>0);
- for index in 1...5
- {
- println("index = \(index)");
- }
- let indicidualscores = [75,43,103,87,12]
- var teamSore = 0
- for score in indicidualscores {
- if score > 50 {
- teamSore += 3;
-
- }else {
- teamSore += 1;
-
- }
- }
- teamSore
-
- var optionalString:String? = "Hello"
- optionalString == nil
- var optionalName:String? = "John Appleseed"
- var greeting = "Hello!"
- if let name = optionalName{
-
- greeting = "hello,\(name)"
-
- }
-
- let vegetable = "red pepper"
- switch vegetable {
- case "celery":
- let vegetableCommment = "Add some raisins and make ant on a log"
- case "cucumber","watercress":
- let vegetablComment = "That would make a good tea sandwich"
- case let x where x.hasSuffix("pepper"):
- let vegetableCommment = "Is it a spicy \(x)"
- default:
- let vegetableComment = "Everything tastes good in soup"
- }
-
- let interestingNumbers = [
- "Prime":[2,3,5,7,11,13],"Fibonnacci":[1,1,2,8],"Square":[1,4,9,16,25],]
- var largrst = 0
- for(kind,numbers) in interestingNumbers{
- for number in numbers{
- if number > largrst{
-
- largrst = number
-
- }
-
- }
-
- }
- largrst
-
- var n = 2
- while n < 100 {
- n = n * 2
- }
- n
-
- var m = 2
- do {
-
- m = m * 2
-
- } while m < 100
- m
-
- var firstForLoop = 0
- for i in 0..3 {
- firstForLoop += i
- }
- firstForLoop
-
- var secondForLoop = 0
- for var i = 0; i < 3; ++i {
- secondForLoop += i
-
-
-
- }
- secondForLoop