Swift數組的迭代訪問

來源:互聯網
上載者:User

標籤:

 1 你可以通過for-in迴圈來迭代訪問整個數組的值。 2 for item in shoppingList { 3     println(item) 4 } 5 // Six eggs 6 // Milk 7 // Flour 8 // Baking Powder 9 // Bananas 10 如果要獲得每個元素的索引及其對應的值,可以使用全域的enumerate方法來迭代使用這個數組。enumerate方法對每個元素都會返回一個由索引值及對應元素值組成的元組。你可以把元組中的成員轉為變數或常量來使用:11 for (index, value) in enumerate(shoppingList) {12     println("Item \(index + 1): \(value)")13 }14 // Item 1: Six eggs15 // Item 2: Milk16 // Item 3: Flour17 // Item 4: Baking Powder18 // Item 5: Bananas 

 

Swift數組的迭代訪問

相關文章

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.