func swapTwoIntValue(a: inout Int, b: inout Int){ let tempValue = a a = b b = tempValue}
var a = 1var b = 2swapTwoIntValue(a: &a, b: &b)PRint(a, b)//2,1
func swapTwoValue<T>(a: inout T, b: inout T){ let tempValue = a a = b b = tempValue}
swapTwoValue(&oneInt, b: &twoInt)print("oneInt:/(oneInt),twoInt:/(twoInt)") // oneInt:3,twoInt:4var oneStr = "hello"var twoStr = "world"swapTwoValue(&oneStr, b: &twoStr)print("oneStr:/(oneStr),twoStr:/(twoStr)")// oneStr:world,twoStr:hellovar oneDouble = 10.01var twoDouble = 20.02swapTwoValue(&oneDouble, b: &twoDouble)print("oneDouble:/(oneDouble),twoDouble:/(twoDouble)")// oneDouble:20.02,twoDouble:10.01
struct IntStack { var items = [Int]() mutating func push(item: Int) { items.append(item) } mutating func pop(item: Int) -> Int { return items.removeLast() }}
struct Stack<T> { var items = [T]() mutating func push(item: T) { items.append(item) } mutating func pop(item: T) -> T { return items.removeLast() }}
var stack = Stack<String>() //要在类型名后面加<类型名>stack.push("uno")stack.push("dos")stack.push("tres")stack.push("cuatro")print(stack.pop()) // cuatro
extension Stack{ //给泛型Stack扩展一个计算型属性topItem,返回最上面的item var topItem: T? { return items.isEmpty ? nil : items[items.count-1] }}
func somFuntion<C:NSObject, P: NSObjectProtocol>(someClass: C, someProtocol: P){ //这里用NSObject和NSObjectProtocol做例子}
func findStrInArray(_ array: [String], strToFind: String) -> Int?{ for (index,value) in array.enumerated(){ if strToFind == value{ return index } } return nil}下面这是针对上面非泛型方法泛型版本的方法
func findIndex<T: Equatable> (_ array: [T], _ valueToFind: T) -> Int? { for (index,value) in array.enumerated(){ if value == valueToFind { //如果没指定S:Equatable 这句话会编译不通过 return index } } return nil}
let value = findIndex([3.14159, 0.1, 0.25], 9.3)// doubleIndex 类型为 Int?,其值为 nil,因为 9.3 不在数组中let stringIndex = findIndex(["Mike", "Malcolm", "Andrea"], "Andrea")// stringIndex 类型为 Int?,其值为 2
protocol Container{ associatedtype itemType //声明一个关联类型 mutating func appended(item: itemType) var count: Int{ get } subscript(i: Int) -> itemType { get }}
struct intStack: Container { // IntStack 的原始实现部分 var items = [Int]() mutating func push(item: Int) { items.append(item) } mutating func pop() -> Int { return items.removeLast() } // Container 协议的实现部分 mutating func appended(item: Int) { self.push(item: item) } var count: Int { return items.count } subscript(i: Int) -> Int { return items[i] }}下面是一个泛型版本的
struct genericStack<T>: Container{ // genericStack<T> 的原始实现部分 var items = [T]() mutating func push(item: T) { items.append(item) } mutating func pop() -> T { return items.removeLast() } // Container 协议的实现部分 mutating func appended(item: T) { self.push(item: item) } var count: Int { return items.count } subscript(i: Int) -> T { return items[i] }}
extension Array :Container{ mutating internal func appended(item: Element) {}}
func allItemsMatch<C1: Container,C2: Container>(someContainer: C1,_ anotherContainer: C2) -> Bool where C1.itemType == C2.itemType, C1.itemType: Equatable { if someContainer.count != anotherContainer.count { return false } for i in 0...someContainer.count-1{ if someContainer[i] != anotherContainer[i]{ return false } } return true}
var stackOfStrings = genericStack<String>()stackOfStrings.appended(item: "uno")stackOfStrings.appended(item: "dos")stackOfStrings.appended(item: "tres")var arrayOfStrings = ["uno", "dos", "tres"] //array类型的满足Container类型,参考上面的extension Arrayif allItemsMatch(stackOfStrings, arrayOfStrings) { print("All items match.")} else { print("Not all items match.")}//结果是:All items match.
新闻热点
疑难解答