话不多说,直接上正题:
使用构造语法创建特定类型的空数组
//type根据自己的需要定义为Int或者String等等
var someArray = [type]()
创建一个初始化大小数组的语法:
//NumbeOfElements为数组的数量,InitialValue为每一位给的初始数据
var thisArray = [type](count: NumbeOfElements, repeatedValue: InitialValue)
比如:
var thatArray = [Int](count: 3, repeatedValue: 0)
以字面量来创建:
var thisArray:[Int] = [1, 2, 3]
访问数组和Object-C一样:
var thisArray:[Int] = [1, 2, 3]
thisArray[1]
修改数组
var thisArray = [Int]()
//添加(你所看到的()和[]都不可省)
thisArray.append(1)
thisArray.append(2)
thisArray += [3]
//修改数组
thisArray[0]=10
便利数组在for 循环中说过,这里再次举例说明:
var thisArray = [String]()
thisArray.append("iOS")
thisArray.append("android")
thisArray.append("h5")
thisArray+= ["PHP"]
for item in thisArray {
print(item)
}
如果我们同时需要每个数据项的值和索引值
for (index, item) in thisArray.enumerate() {
print("在 index = \(index) 位置上的值为 \(item)")
}
数组的删除方法:
removeAtIndex()
removeFirst()
removeAll()
removeLast()
合并数组:数组A + 数组B
数组有isEmpty方法,返回true或false
数组保留了.count方法,得到数组的数量