数组使用有序列表存储同一类型的多个值,且相同的值可以多次出现在一个数组的不同位置中。
数组会强制检测元素的类型,如果类型不同则会报错,Swift数组应该遵循像Array<Element>这样的形式,其中Element是这个数组中唯一允许存在的数据类型。
如果创建一个数组,并赋值给一个变量,则创建的集合就是可以修改的。这意味着在创建数组后,可以通过添加、删除、修改的方式改变数组里的项目。如果将一个数组赋值给常量,数组就不可更改,并且数组的大小和内容都不可以修改。
使用注意事项: 数组初始化时定义了的元素类型后,在后续使用中,元素类型必须是统一的。但如果定义了类型为"Any",或"AnyObject"时,则数组中可以使用任意类型的元素,同时"Any"类型时,还可以包含方法selector类。
-
-
let arrayTmp01 = Array<String>()
-
print(arrayTmp01)
-
-
let arrayTmp02 = [String]()
-
print(arrayTmp02)
-
-
let arrayTmp03:Array<String> = []
-
print(arrayTmp03)
-
-
var arrayTmp04 = ["1", "2"]
-
print(arrayTmp04)
-
-
arrayTmp04 = []
-
print(arrayTmp04)
-
-
arrayTmp04 = ["1", "2"]
-
print(arrayTmp04)
-
-
arrayTmp04 = Array<String>()
-
print(arrayTmp04)
-
-
let array01:[Int] = [1,2,3,4,5]
-
print(array01)
-
-
let array02 = ["1", "2", "3"]
-
print(array02)
-
-
let array03 = [array01, array02]
-
print(array03)
-
-
let value01 = array01[0]
-
print(value01)
-
-
let value02 = array01.indexOf(1)
-
print(value02)
-
-
for value in array01
-
{
-
print("value = \(value)")
-
}
-
-
for var index = 0; index < array01.count; index++
-
{
-
let value = array01[index]
-
print("index = \(index), value = \(value)")
-
}
-
-
for i in 0..<array01.count
-
{
-
print(array01[i])
-
}
-
-
for (index, item) in array01.enumerate()
-
{
-
print("在 index = \(index) 位置上的值为 \(item)")
-
}
-
-
var array04 = [10,20,30]
-
-
array04.append(100)
-
print(array04)
-
array04 = array04 + [200]
-
print(array04)
-
array04.appendContentsOf([201,202])
-
print(array04)
-
array04.appendContentsOf(203...210)
-
print(array04)
-
array04.insert(500, atIndex: 2)
-
print(array04)
-
-
array04[4] = 300
-
print(array04)
-
-
array04.removeFirst()
-
print(array04)
-
-
array04.removeLast()
-
print(array04)
-
-
array04.removeAtIndex(0)
-
print(array04)
-
-
array04.removeAll()
-
print(array04)
-
-
array04 = array01 + [100,200,300]
-
print(array04)
-
-
var isArray:Bool = array01 is Array
-
if isArray
-
{
-
print("array01 is array class")
-
}
-
else
-
{
-
print("array01 is not array class")
-
}
-
-
let count01 = array04.count
-
print(count01)
-
-
let isValid = array04.isEmpty
-
print(isValid)
-
-
let item01 = array04.first
-
print(item01)
-
-
let item02 = array04.last
-
print(item02)
-
-
let contant1:Bool = array01.contains(1)
-
print("array01 \(contant1) 包含 1")
-
-
let arraySort = array04.sort({(n1:Int, n2:Int)->Bool in
-
-
-
-
-
return n2 < n1
-
})
-
print(arraySort)
-
-
let arraySort02 = array04.sort({
-
-
-
-
-
return $1 < $0
-
})
-
print(arraySort02)
-
-
let arraySort03 = array04.sort(){
-
-
-
-
-
$1 < $0
-
}
-
print(arraySort03)
补充:Any类型元素,或AnyObject类型元素的数组
-
-
func buttonClick() -> Void
-
{
-
print("点击")
-
}
-
print(buttonClick)
-
-
-
let arrayAnyObject01:Array<AnyObject> = [Grade0ne(chinese: "86", maths: "66"), GradeTwo(chinese: "78", english: "92"), "DevZhang", 30]
-
print(arrayAnyObject01)
-
-
-
let arrayAnyObject02:Array<Any> = [Grade0ne(chinese: "86", maths: "66"), GradeTwo(chinese: "78", english: "92"), "DevZhang", 30, buttonClick()]
-
print(arrayAnyObject02)
作者:st646889325 发表于2016/10/17 11:24:20
原文链接