前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >Swift---泛型(Generics)

Swift---泛型(Generics)

作者头像
用户3004328
发布于 2018-09-06 08:43:22
发布于 2018-09-06 08:43:22
40100
代码可运行
举报
文章被收录于专栏:增长技术增长技术
运行总次数:0
代码可运行

泛型使您能够编写灵活的、可重用的功能和类型的代码。

例如要交换两个变量值的问题

不用泛型

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
//Int类型交换
func swapTwoInts(inout a: Int, inout b: Int){
  let temp = a
  a = b
  b = temp
}

var someInt = 3
var anotherInt = 107
swapTwoInts(&someInt, &anotherInt)
println("someInt is now \(someInt), and anotherInt is now \(anotherInt)")

// prints "someInt is now 107, and anotherInt is now 3"

//String类型交换
func swapTwoStrings(inout a: String, inout b: String){
  let temp = a
  a = b
  b = temp
}

//Double类型交换
func swapTwoDoubles(inout a: Double, inout b: Double){
  let temp = a
  a = b
  b = temp
}

使用泛型

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
func swapTwoValues<T>(inout a: T, inout b: T){
  let temp = a
  a = b
  b = temp
}

var someInt = 3
var anotherInt = 107
swapTwoValues(&someInt, &anotherInt)
// someInt is now 107, and anotherInt is now 3

var someString = "hello"
var anotherString = "world"
swapTwoValues(&someString, &anotherString)
// someString is now "world", and anotherString is now "hello"

Swift中自带的Array和Dictionary都是使用泛型实现的,下面通过泛型自定义简单的Stack

不用泛型

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
struct IntStack{
  var items = Int[]()
  mutating func push(item: Int){
    item.append(item)
  }

  mutating func pop() -> Int {
    return items.removeLast()
  }
}

使用泛型

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
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

let fromTheTop = stackOfStrings.pop()
// fromTheTop is equal to "cuatro", and the stack now contains 3 strings

类型限制

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
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)")
}

// prints "The index of llama is 2"

func findIndex<T>(array: T[], valueToFind: T) -> Int? {
  for(index, value) in enumerate(array) {
    if value == valueToFind {
      return index
    }
  }
  return nil
}

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 array

let stringIndex = findIndex(["Mike", "Malcolm", "Andrea"], "Andrea")
// stringIndex is an optional Int containing a value of 2
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2014-06-09,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
本文部分代码块支持一键运行,欢迎体验
本文部分代码块支持一键运行,欢迎体验