技术文章分类(180)

技术随笔(11)

swift的语法小demo6(数组)

 

//
//  main.swift
//  SwiftGrammarStudy
//
//  Created by dongway on 14-6-6.
//  Copyright (c) 2014年 dongway. All rights reserved.
//

import Foundation

/*
    数组
*/
var shoppingList = String[]()
shoppingList = ["eggs","milk"]
println("The shopping list contains \(shoppingList.count) items.")
if shoppingList.isEmpty {
    println("The shopping list is empty.")
} else {
    println("The shopping list is not empty.")
}

shoppingList.append("Flour")
// shoppingList now contains 3 items, and someone is making pancakes”
shoppingList += "Baking Powder"
// shoppingList now contains 4 items”
shoppingList += ["Chocolate Spread", "Cheese", "Butter"]
// shoppingList now contains 7 items”
shoppingList[4..6] = ["Bananas", "Apples"]
// shoppingList now contains 7 items”
println("\(shoppingList)")
shoppingList[4...6] = ["Bananas", "Apples"]
// shoppingList now contains 6 items”
println("\(shoppingList)")
shoppingList.insert("Maple Syrup", atIndex: 0)

let apples = shoppingList.removeLast()
var apples2 = shoppingList.removeAtIndex(1)

//迭代
for item in shoppingList {
    println(item)
}
for (index, value) in enumerate(shoppingList) {
    println("Item \(index + 1): \(value)")
}

//数组初始化
var someInts = Int[]()
println("someInts is of type Int[] with \(someInts.count) items.")
// prints "someInts is of type Int[] with 0 items.”
someInts.append(3)
// someInts now contains 1 value of type Int
someInts = []
// someInts is now an empty array, but is still of type Int[]”
var threeDoubles = Double[](count: 3, repeatedValue: 0.0)
// threeDoubles is of type Double[], and equals [0.0, 0.0, 0.0]”
var anotherThreeDoubles = Array(count: 3, repeatedValue: 2.5)
// anotherThreeDoubles is inferred as Double[], and equals [2.5, 2.5, 2.5]”
var sixDoubles = threeDoubles + anotherThreeDoubles
// sixDoubles is inferred as Double[], and equals [0.0, 0.0, 0.0, 2.5, 2.5, 2.5]”

 

posted @ 2014-06-12 11:15  坤哥MartinLi  阅读(145)  评论(0编辑  收藏  举报