swift 易忽略的笔记 10):Generic
来源:互联网 发布:简单快递单打印软件 编辑:程序博客网 时间:2024/04/29 05:58
1. Generic Functions
func swapTwoValues<T>(inout a: T, inout b: T) { let temporaryA = a a = b b = temporaryA}
var someInt = 3var anotherInt = 107swapTwoValues(&someInt, &anotherInt)// someInt is now 107, and anotherInt is now 3
2. Stack
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")// the stack now contains 4 strings”
3. Type Constraints
func findIndex<T: Equatable>(array: T[], valueToFind: T) -> Int? { for (index, value) in enumerate(array) { if value == valueToFind { return index } } return nil}
let doubleIndex = findIndex([3.14159, 0.1, 0.25], 9.3)// doubleIndex is an optional Int with no value, because 9.3 is not in the arraylet stringIndex = findIndex(["Mike", "Malcolm", "Andrea"], "Andrea")// stringIndex is an optional Int containing a value of 2
4. Associated Types (for protocols)
protocol Container { typealias ItemType mutating func append(item: ItemType) var count: Int { get } subscript(i: Int) -> ItemType { get }}struct IntStack: Container { // original IntStack implementation var items = Int[]() mutating func push(item: Int) { items.append(item) } mutating func pop() -> Int { return items.removeLast() } // conformance to the Container protocol typealias ItemType = Int mutating func append(item: Int) { self.push(item) } var count: Int { return items.count } subscript(i: Int) -> Int { return items[i] }}struct Stack<T>: Container { // original Stack<T> implementation var items = T[]() mutating func push(item: T) { items.append(item) } mutating func pop() -> T { return items.removeLast() } // conformance to the Container protocol mutating func append(item: T) { self.push(item) } var count: Int { return items.count } subscript(i: Int) -> T { return items[i] }}
4. Where Clause
func allItemsMatch< C1: Container, C2: Container where C1.ItemType == C2.ItemType, C1.ItemType: Equatable> (someContainer: C1, anotherContainer: C2) -> Bool { // check that both containers contain the same number of items if someContainer.count != anotherContainer.count { return false } // check each pair of items to see if they are equivalent for i in 0..someContainer.count { if someContainer[i] != anotherContainer[i] { return false } } // all items match, so return true return true}
0 0
- swift 易忽略的笔记 10):Generic
- Swift 易忽略的笔记 1)
- Swift 易忽略的笔记 2):String
- Swift 易忽略的笔记 3): Array & Dictionary
- Swift 易忽略的笔记 4):Function & Closure
- Swift 易忽略的笔记 6):Methods & Subscripts
- Swift 易忽略的笔记 8):Extensions
- Swift 易忽略的笔记 9):Protocol
- swift 易忽略的笔记 11):Operator Functions
- swift 易忽略的笔记 12): Miscellaneous
- Swift 易忽略的笔记 5):Enumeration & Classes and Structs & Properties总结
- Swift 易忽略的笔记 7):Type Casting & Nested Type
- iOS --- Swift中泛型(Generic)的基本使用
- Swift 泛型参数(Generic Parameters and Arguments)
- 泛型(generic)的概念
- java的generic(泛型)
- C++ 学习笔记一易忽略的地方
- Swift学习笔记(一)——Swift的数据类型
- 3.14 MonoForAndroid用户人机界面--控制不同的文字字体
- 硬盘丢失了怎么才能恢复呢丨顶尖数据恢复软件恢复硬盘数据丨
- NGUI中的depth
- 在操作系统Centos 6.3 上使用Nexus 2.8搭建MAVEN私服 及 cloudstack 开发环境
- mysql 将时间戳直接转换成日期时间
- swift 易忽略的笔记 10):Generic
- 通用的Java hashCode重写方案
- 11gR2 RAC RMAN备份问题------手动删除归档日志导致RMAN-06059 ORA-19625
- Android WebView的坑总结
- 2013年进口的乳品不合格
- android textview xml 属性设置
- 一般电脑丢失文件怎么才能迅速恢复丨迅龙数据恢复文件丨
- android如何杀掉守护进程——干掉小米手机助手
- websphere服务器cpu使用率较高原因定位分析