Swift基礎資料類型和迴圈

來源:互聯網
上載者:User

標籤:http   io   資料   for   art   ar   cti   html   

本文是一篇swift的基礎教材,講解了swift語言的變數,資料類型和迴圈.準備學swift的同學可以參考學習下.

1 常量變數

    var myVariable = 42 //變數  
    myVariable = 50  
    let myConstant = 42 //常量  


2 顯示指明變數類型


    let explicitDouble: Double = 70  


3 數組和字典


    var shoppingList = ["catfish", "water", "tulips", "blue paint"]   
    shoppingList[1] = "bottle of water"   
        
    var occupations = [   
        "Malcolm": "Captain", //前面是key 後面是value  
        "Kaylee": "Mechanic",   
    ]   
    occupations["Jayne"] = "Public Relations"  


4 建立初始化數組和字典


    let emptyArray = String[]()   
    let emptyDictionary = Dictionary<String, Float>()   

5數組添加


    var emptyarry = String[]()  
    emptyarry.append("dfd")  

6 for迴圈


    let sss = [11,12,43,554,12,434]  
    for i in 0..4 {  
         print("(i) = (sss[i])");  
             print("n");  
          }  
    for i in sss{  
          print(i);  
             print("n");  
          }  


7 switch

    let vegetable = "red pepper"   
    switch vegetable {   
    case "celery":   
        let vegetableComment = "Add some raisins and make ants on a log."   
    case "cucumber", "watercress":   
        let vegetableComment = "That would make a good tea sandwich."   
    case let x where x.hasSuffix("pepper"):   
        let vegetableComment = "Is it a spicy (x)?" fallthrough   
    default:   
        let vegetableComment = "Everything tastes good in soup."   
    }  

自:http://www.phperz.com/article/14/0802/15200.html

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.