Swift-数组跟字典(一)

Swift--数组和字典(一)

数组是一个存储多个相同类型的值的有序列表。相同的值,可以在不同的位置出现在一个数组中的多个次。
Swift数组是具体的。他不同于Objective-C的的NSArray和NSMutableArray里的类,它可以存储任何类型的对象,不提供有关它们返回的对象的性质的任何信息。在斯Swift,一个特定的数组可以存储的值类型总是明确的,无论是通过显式类型批注,或通过类型推断,而不一定是类类型。如果创建诠释值的数组,例如,你不能插入比Int值以外的任何值到该数组。Swift数组是类型安全的,并且总是清楚它们可能含有什么。

 

数组语法

数组的类型是Array<SomeType>,其中个SomeType是该数组允许存储的类型。你也可以写简写形式数组的类型为个SomeType[]。虽然两种形式在功能上是相同的,简写形式是首选,而指的是一个数组类型时使用本指南。

 

 

您可以初始化数组与数组文本,这是一种速记的方式来写一个或多个值作为数组集合。数组常写为值列表,以逗号分隔,由一对方括号包围:

[value1,value2, value3]

实例如下:

.        var shoppingList: String[] = ["Eggs", "Milk"]

.        //shoppingList has been initialized with two initial items

该shoppingList变量声明为“字符串值的数组”,写为String[]。因为这个特殊的数组指定字符串的值类型,它仅允许存储字符串值。在这里,shoppingList数组与两个字符串值(“Eggs”和“Milk”)进行初始化,写入数组字面之内。

 

由于Swift的类型推断,你可以不用写入数组的类型,如果你初始化它与同类型的数组字面含值。 shoppingList的初始化可以写一个较短的形式:

var shoppingList = ["Eggs","Milk"]

由于数组中都是字符串类型,所以可以如上这么写.

 

 

访问和修改数组

我们可以通过数组的方法和属性,也可以通过语法来实现.

我们可以通过只读的count属性来获取count值.

.      println("The shopping listcontains \(shoppingList.count) items.")

.       // prints "The shopping list contains2 items."

通过数组的isEmpty属性来检测是否为空

 

 

 

if shoppingList.isEmpty {

    println("The shopping list is empty.")

} else{

println("The shoppinglist is not empty.")

 

}
// prints "Theshopping list is not empty."

 

你可以通过append在数组最后增加一个元素

 shoppingList.append("Flour")

 //shoppingList now contains 3 items, and someone is making pancakes

你也可以通过+=来实现增加一个元素到数组最后

.      shoppingList += "Baking Powder"

.       // shoppingList now contains 4 items

 

.      shoppingList += ["ChocolateSpread", "Cheese", "Butter"]

  //shoppingList now contains 7 items

通过使用语法标从数组中检索值,传递你要检索中的值的索引
立即数组的名称之后方括号:

.      var firstItem = shoppingList[0]

.        //firstItem is equal to "Eggs"

请注意,数组中的第一项为0,而不是1。Swift数组是始终为零索引。

shoppingList[0] = "Six eggs"

// the first item in the list is now equal to "Sixeggs" rather than "Eggs"

你也可以使用下标语法来改变一个范围值一次,即使更换一组值有

不同长度的比你要更换的范围内。下面的示例将“Chocolate Spread”,“Cheese”,

和“Butter”与“Bananas”和“Apples”:

shoppingList[4...6]= ["Bananas", "Apples"]

//shoppingList now contains 6 items

 

插入一个项目到数组中指定索引处,调用数组的insert(atIndex:)方法:

shoppingList.insert("MapleSyrup", atIndex: 0)

//shoppingList now contains 7 items

//"Maple Syrup" is now the first item in the list

 

同样,你可以使用removeAtIndex方法删除数组中一个项目。此方法删除了该项目的指定索引,并返回被删除的项目(尽管你可以忽略返回值,如果你不需要它):

letmapleSyrup = shoppingList.removeAtIndex(0)

//the item that was at index 0 has just been removed

//shoppingList now contains 6 items, and no Maple Syrup

//the mapleSyrup constant is now equal to the removed "Maple Syrup"string

如果你想从数组中删除最后一个项目,使用removeLast方法,而不是removeAtIndex

方法以避免需要查询数组的Count属性。像removeAtIndex方法,removeLast

返回已删除项目:

letapples = shoppingList.removeLast()

//the last item in the array has just been removed

//shoppingList now contains 5 items, and no cheese

//the apples constant is now equal to the removed "Apples" string

 

迭代数组

你可以使用for-in来完成数组迭代

foritem in shoppingList {  

 println(item)

}

//Six eggs

//Milk

//Flour

//Baking Powder

//Bananas

 

如果你需要的每个项目,以及该整数索引的值,使用全局枚举函数来遍历

数组代替。该枚举函数返回一个元组每个项目的指标组成的数组中和

值该项目。你可以分解成元组临时常量或变量作为迭代的一部分:

for(index, value) in enumerate(shoppingList) {

    println("Item \(index + 1):\(value)")

}

//Item 1: Six eggs

//Item 2: Milk

//Item 3: Flour

//Item 4: Baking Powder

//Item 5: Bananas

 

数组的初始化

你可以给一个数组确定的类型:

varsomeInts = Int[]()

println("someIntsis 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[]

Swift的数组类型还提供了一个初始化用于创建具有一定规模与所有其值设置为提供默认值的数组。你通过这个初始化器被添加到新的数组(名为count)的项目数和

适当的类型(称为repeatedValue)的默认值:

varthreeDoubles = Double[](count: 3, repeatedValue: 0.0)

//threeDoubles is of type Double[], and equals [0.0, 0.0, 0.0]

由于类型推断,就不需要指定使用该初始值,当要被存储在数组中的类型,

因为它可以从默认值来推断:

varanotherThreeDoubles = Array(count: 3, repeatedValue: 2.5)

//anotherThreeDoubles is inferred as Double[], and equals [2.5, 2.5, 2.5]

最后,你可以通过与另外加在一起兼容类型的两个现有阵列创建一个新的数组

运算符(+)。新数组的类型是从两个数组你加在一起的类型推断:

varsixDoubles = threeDoubles + anotherThreeDoubles

//sixDoubles is inferred as Double[], and equals [0.0, 0.0, 0.0, 2.5, 2.5, 2.5]