import Foundation@H_404_7@
@H_404_7@
@H_404_7@
func swapTwoInts(inout a: Int,inout b: Int) {@H_404_7@
let temporaryA = a@H_404_7@
a = b@H_404_7@
b = temporaryA@H_404_7@
}@H_404_7@
@H_404_7@
func swapTwoStrings(inout a: String,inout b: String) {@H_404_7@
let temporaryA = a@H_404_7@
a = b@H_404_7@
b = temporaryA@H_404_7@
}@H_404_7@
@H_404_7@
func swapTwoDoubles(inout a: Double,inout b: Double) {@H_404_7@
let temporaryA = a@H_404_7@
a = b@H_404_7@
b = temporaryA@H_404_7@
}@H_404_7@
@H_404_7@
@H_404_7@
//泛型代码可以让你写出根据自我需求定义适用于任何类型的,灵活且可重用的函数和类型。它可以让你避免重复代码,用一种清晰和抽象的方式来表达代码的意图@H_404_7@
@H_404_7@
//T为占位类型名字,可支持多个类型参数,命名在尖括号中,用逗号分开@H_404_7@
func swapTwoValues<T>(inout a: T,inout b: T) {@H_404_7@
let temporaryA = a@H_404_7@
a = b@H_404_7@
b = temporaryA@H_404_7@
}@H_404_7@
@H_404_7@
var someInt = 3@H_404_7@
var anotherInt = 107@H_404_7@
swapTwoValues(&someInt,&anotherInt)@H_404_7@
@H_404_7@
var someString = "hello"@H_404_7@
var anotherString = "world"@H_404_7@
swapTwoValues(&someString,&anotherString)@H_404_7@
@H_404_7@
@H_404_7@
@H_404_7@
//MARK: - 泛型类型与泛型参数@H_404_7@
struct IntStack {@H_404_7@
var items = [Int]()@H_404_7@
mutating func push(item: Int) {@H_404_7@
items.append(item)@H_404_7@
}@H_404_7@
mutating func pop() -> Int {@H_404_7@
return items.removeLast()@H_404_7@
}@H_404_7@
}@H_404_7@
@H_404_7@
struct Stack<T> {@H_404_7@
var items = [T]()@H_404_7@
mutating func push(item: T) {@H_404_7@
items.append(item)@H_404_7@
}@H_404_7@
mutating func pop() -> T {@H_404_7@
return items.removeLast()@H_404_7@
}@H_404_7@
}@H_404_7@
@H_404_7@
var stackOfStrings = Stack<String>()@H_404_7@
stackOfStrings.push("uno")@H_404_7@
stackOfStrings.push("dos")@H_404_7@
stackOfStrings.push("tres")@H_404_7@
stackOfStrings.push("cuatro")@H_404_7@
// 现在栈已经有4个string了@H_404_7@
let fromTheTop = stackOfStrings.pop()@H_404_7@
@H_404_7@
@H_404_7@
@H_404_7@
//MARK: - 类型约束@H_404_7@
//类型约束指定了一个必须继承自指定类的类型参数,或者遵循一个特定的协议或协议构成@H_404_7@
/*@H_404_7@
//第一个类型参数T,有一个需要T必须是SomeClass子类的类型约束;第二个类型参数U,有一个需要U必须遵循SomeProtocol协议的类型约束@H_404_7@
func someFunction<T: SomeClass,U: SomeProtocol>(someT: T,someU: U) {@H_404_7@
// function body goes here@H_404_7@
}@H_404_7@
*/@H_404_7@
@H_404_7@
func findStringIndex(array: [String],valueToFind: String) -> Int? {@H_404_7@
for (index,value) in enumerate(array) {@H_404_7@
if value == valueToFind {@H_404_7@
return index@H_404_7@
}@H_404_7@
}@H_404_7@
return nil@H_404_7@
}@H_404_7@
let strings = ["cat","dog","llama","parakeet","terrapin"]@H_404_7@
if let foundIndex = findStringIndex(strings,"llama") {@H_404_7@
println("The index of llama is \(foundIndex)")@H_404_7@
}@H_404_7@
// 输出 "The index of llama is 2"@H_404_7@
@H_404_7@
@H_404_7@
//Swift 标准库中定义了一个Equatable协议,该协议要求任何遵循的类型实现等式符(==)和不等符(!=)对任何两个该类型进行比较。所有的 Swift 标准类型自动支持Equatable协议。@H_404_7@
func findIndex<T: Equatable>(array: [T],valueToFind: T) -> Int? {@H_404_7@
for (index,value) in enumerate(array) {@H_404_7@
if value == valueToFind { //不是所有的 Swift 中的类型都可以用等式符(==)进行比较@H_404_7@
return index@H_404_7@
}@H_404_7@
}@H_404_7@
return nil@H_404_7@
}@H_404_7@
@H_404_7@
let doubleIndex = findI@H_404_7@