Swift - 数组

话不多说,直接上正题:

使用构造语法创建特定类型的空数组

//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方法,得到数组的数量

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

CodingFire

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值