集合
集合
目录
1.数组
1.1创建数组
Swift语言使用[T]来建华标准库中定义的命名型类型Array<T>。
1.使用构造函数创建数组
var shoppingList = Array < String >()
// 或者
var shoppingList = [ String ]()
2.使用字面量创建数组
var shoppingList :[ String ]=[ "Eggs" , "Milk" ];
//通过类型判断可以省略类型
var shoppingList = [ "Eggs" , "Milk" ];
3.Swift还提供一个可以创建特定大小并且所有数据都被默认的构造方法
//Swift还提供一个可以创建特定大小并且所有数据都被默认的构造方法
var threeDoubles = [ Double ]( count : 3 , repeatedValue : 0.0 )
// threeDoubles 是一种 [Double]数组, 等于 [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
1.2访问和使用数组
数组通过count属性获取数组个数
println ( "shoppingList count is \( shoppingList . count ) " )
//输出shoppingList count is 2
通过isEmpty属性来判断数组是否为空
//通过isEmpty属性来判断数组是否为空
if shoppingList . isEmpty {
println ( "The shopping list is empty." )
} else {
println ( "The shopping list is not empty." )
}
//输出The shopping list is not empty.
1.2.1添加项
1.通过append添加新项
shoppingList . append ( "Flour" )
2.通过+=运算符来添加新项
shoppingList += [ "Baking Powder" ]
shoppingList += [ "Chocolate Spread" , "Cheese" , "Butter" ]
3.使用insert方法来插入新项目
//调用数组的insert(atIndex:)方法来在某个具体索引值之前添加数据项
shoppingList2 . insert ( "Maple Syrup" , atIndex : 0 )
1.2.2访问和修改项
使用下标访问数组
使用下标获取项并赋值
shoppingList [ 4. .. 6 ]=[ "Bananas" , "Apples" ];
shoppingList [ 0 ]= "Oranges"
1.2.3移除项
调用removeAtIndex移除指定项目
//使用removeAtIndex方法来移除数组中的某一项
let mapleSyrup = shoppingList . removeAtIndex ( 0 )
调用removeLast来移除最后一项
//使用removeLast方法来移除最后一项
let apples = shoppingList . removeLast ()
1.3遍历数组
1.使用for-in遍历数组
for item in shoppingList2 {
println ( item )
}
//Oranges
//Milk
//Flour
//Baking Powder
//Bananas
2.使用enumerate遍历,enumerate返回一个由每个数据项索引值和数据组成的元组
//2. 使用enumerate遍历
//enumerate返回一个由每一个数据项索引值和数据值组成的元组
for ( index , value ) in enumerate ( shoppingList2 ) {
println ( "Item \( index + 1 ) : \( value ) " )
}
//Item 1: Oranges
//Item 2: Milk
//Item 3: Flour
//Item 4: Baking Powder
//Item 5: Bananas
2.字典
2.1创建字典
Swift语言使用[keyType:valueType]来简化标准库中定义的命名型类型Dictionary<keyType,valueType>。
1.使用构造函数创建
var nameOfIntegers = Dictionary < Int , String >()
nameOfIntegers [ 16 ] = "sixteen"
// 或者
var nameOfIntegers = [ Int : String ]()
nameOfIntegers [ 16 ] = "sixteen"
2.使用字面量创建
var airports :[ String : String ] = [ "TYO" : "Tokyo" , "DUB" : "Dublin" ]
//根据类型判断可以省略类型
var airports = [ "TYO" : "Tokyo" , "DUB" : "Dublin" ]
2.2使用字典
count属性获取字典数量
//count属性来获取字典数量
println ( "The dictionary of airports contains \( airports . count ) items." )
isEmpty判断是否为空
if airports . isEmpty {
println ( "The airports dictionary is empty." )
} else {
println ( "The airports dictionary is not empty." )
}
2.2.1添加项
updateValue(forKey:)方法在这个键不存在对应值的时候设置值或者在存在时更新已存在的值,这个方法返回更新值之前的原值。
if let oldValue = airports2 . updateValue ( "Dublin Internation" , forKey : "DUB" ) {
println ( "The old value for DUB was \( oldValue ) ." )
}
//打印The old value for DUB was Dublin.
2.
//对应的key不存在则添加,存在则重新赋值
airports [ "ABC" ] = "ABC"
2.2.2移除项
1.使用下标语法来通过给某个键的对应值赋值为nil来从字典里移除一个键值对:
2.用removeValueForKey方法移除键值对
这个方法在键值对存在的情况下会移除该键值对并且返回被移除的value或者在没有值的情况下返回nil
if let removedValue = airports . removeValueForKey ( "DUB" ) {
println ( "The removed airport's name is \( removedValue ) ." )
} else {
println ( "The airports dictionary does not contain a value for DUB." )
}
// 输出 The removed airport's name is Dublin Internation.
2.3遍历
1.使用for-in循环来遍历某个字典中的键值对。
每一个字典中的数据项都由(key, value)元组形式返回
for ( airportCode , airportName ) in airports {
println ( " \( airportCode ) : \( airportName ) " )
}
2.通过访问它的keys或者values属性(都是可遍历集合)检索一个字典的键或者值:
for airportCode in airports . keys {
println ( "Airport code: \( airportCode ) " )
}
for airportName in airports . values {
println ( "Airport name: \( airportName ) " )
}
3.可以直接使用keys或者values属性直接构造一个新数组
let airportCodes = Array ( airports . keys )
// airportCodes is ["TYO", "LHR"]
let airportNames = Array ( airports . values )
September 28, 2026
May 18, 2015
GitHub