swift与pattern

import Foundation



func swapTwoInts(inout a: Int,inout b: Int) {

let temporaryA = a

a = b

b = temporaryA

}


func swapTwoStrings(inout a: String,inout b: String) {

let temporaryA = a

a = b

b = temporaryA

}


func swapTwodoubles(inout a: Double,inout b: Double) {

let temporaryA = a

a = b

b = temporaryA

}



//泛型代码可以让你写出根据自我需求定义适用于任何类型的,灵活且可重用的函数和类型。它可以让你避免重复代码,用一种清晰和抽象的方式来表达代码的意图


//T为占位类型名字,可支持多个类型参数,命名在尖括号中,用逗号分开

func swapTwovalues<T>(inout a: T,inout b: T) {

let temporaryA = a

a = b

b = temporaryA

}


var someInt = 3

var anotherInt = 107

swapTwovalues(&someInt,&anotherInt)


var someString = "hello"

var anotherString = "world"

swapTwovalues(&someString,&anotherString)




//MARK: - 泛型类型与泛型参数

struct IntStack {

var items = [Int]()

mutating func push(item: Int) {

items.append(item)

}

mutating func pop() -> Int {

return items.removeLast()

}

}


struct Stack<T> {

var items = [T]()

mutating func push(item: T) {

items.append(item)

}

mutating func pop() -> T {

return items.removeLast()

}

}


var stackOfStrings = Stack<String>()

stackOfStrings.push("uno")

stackOfStrings.push("dos")

stackOfStrings.push("tres")

stackOfStrings.push("cuatro")

// 现在栈已经有4string

let fromThetop = stackOfStrings.pop()




//MARK: - 类型约束

//类型约束指定了一个必须继承自指定类的类型参数,或者遵循一个特定的协议或协议构成

/*

//一个类型参数T,有一个需要T必须是SomeClass子类的类型约束;第二个类型参数U,有一个需要U必须遵循SomeProtocol协议的类型约束

func someFunction<T: SomeClass,U: SomeProtocol>(someT: T,someU: U) {

// function body goes here

}

*/


func findStringIndex(array: [String],valuetoFind: String) -> Int? {

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

if value == valuetoFind {

return index

}

}

return nil

}

let strings = ["cat","dog","llama","parakeet","terrapin"]

if let foundindex = findStringIndex(strings,"llama") {

println("The index of llama is \(foundindex)")

}

// 输出 "The index of llama is 2"



//Swift 标准库中定义了一个Equatable协议,该协议要求任何遵循的类型实现等式符(==)和不等符(!=)对任何两个该类型进行比较。所有的 Swift 标准类型自动支持Equatable协议。

func findindex<T: Equatable>(array: [T],valuetoFind: T) -> Int? {

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

if value == valuetoFind { //不是所有的 Swift 中的类型都可以用等式符(==)进行比较

return index

}

}

return nil

}


let doubleIndex = findI

相关文章

软件简介:蓝湖辅助工具,减少移动端开发中控件属性的复制和粘...
现实生活中,我们听到的声音都是时间连续的,我们称为这种信...
前言最近在B站上看到一个漂亮的仙女姐姐跳舞视频,循环看了亿...
【Android App】实战项目之仿抖音的短视频分享App(附源码和...
前言这一篇博客应该是我花时间最多的一次了,从2022年1月底至...
因为我既对接过session、cookie,也对接过JWT,今年因为工作...