Swift   发布时间:2022-03-31  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Swift 泛型(十九)大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

概述

泛型 泛型代码可根据自定义需求,写出适用于任何类型、灵活且可重用的函数和类型,避免重复的代码,用一种清晰和抽象的思维表达代码的意思 1.泛型用途 [objc]  view plain  copy   // 普通的函数,用来交换两个值   func swapTwoInts(inout a: Int, inout b: Int) {       let temporaryA = a       a =
@H_502_0@泛型

泛型代码可根据自定义需求,写出适用于任何类型、灵活且可重用的函数和类型,避免重复的代码,用一种清晰和抽象的思维表达代码的意思

1.泛型用途

[objc] view plain copy
  1. //普通的函数,用来交换两个值
  2. funcswapTwoInts(inouta:Int,inoutb:Int){
  3. lettemporaryA=a
  4. a=b
  5. b=temporaryA
  6. }
  7. varsomeInt=3
  8. varanotherInt=107
  9. swapTwoInts(&someInt,&anotherInt)
  10. println("someIntisNow\(someInt),andanotherIntisNow\(anotherInt)")
  11. //prints"someIntisNow107,andanotherIntisNow3"
  12. funcswapTwoStrings(inouta:String,0); background-color:inherit">b:String){
  13. funcswapTwoDoubles(inouta:Double,0); background-color:inherit">b:Double){
  14. }
以上都是转换两个数的值,但他们代码都是一样的,只不过传入类型不一样,同一份代码就要写了三次,使用泛型就可以忽略传入值的类型,只使用一份代码

2.泛型函数

占位符T是一种类型参数的示例,类型参数指定命名为一个占位类型,并且紧随在函数名后面,使用一对尖括号括起来,一旦类型参数被确定,就可以用来定义函数的参数类型(a,b),或作为函数的返回类型,或作为函数主题的注释类型,此时类型参数所代表的占位符不管函数任何时候被调用,都会被实际类型所替代
copy
@L_419_6@
funcswapTwoInts(inoutb:Int)
  • funcswapTwoValues<T>(inoutb:T)
  • swapTwoValues(&someInt,0); background-color:inherit">//someIntisnow107,andanotherIntisnow3
  • varsomeString="hello"
  • varanotherString="world"
  • swapTwoValues(&someString,&anotherString)
  • //someStringisnow"world",andanotherStringisnow"hello"

  • 3.泛型类型

    Swift允许自定义泛型类型,这些自定义类、结构体和枚举作用于任何类型
    copy
    //以下是一个用泛型写的栈Stack,模拟栈的push和pop
  • structIntStack{
  • varitems=Int[]()
  • mutatingfuncpush(item:Int){
  • items.append(item)
  • mutatingfuncpop()->Int{
  • returnitems.removeLast()
  • }
  • //Stack提供两个方法,push和pop,从栈中压进一个值和弹出一个值,但只能用于int值,下面定义一个泛型Stack类,可处理任何类型的栈
  • structStack<T>{
  • varitems=T[]()
  • mutatingfuncpush(item:T){
  • items.append(item)
  • mutatingfuncpop()->T{
  • returnitems.removeLast()
  • varstackOfStrings=Stack<String>()
  • stackOfStrings.push("uno")
  • stackOfStrings.push("dos")
  • stackOfStrings.push("tres")
  • stackOfStrings.push("cuatro")
  • //thestacknowcontains4strings
  • letfromTheTop=stackOfStrings.pop()
  • //fromTheTopisequalto"cuatro",andthestacknowcontains3strings

  • 4.类型约束

    有时候对使用在泛型函数和泛型类型上的类型强制约束为某种特定的类型是非常有用的,可以指定一个必须继承自指定类的类型参数,或者遵循一个特定的协议或协议构成

    4.1语法


    4.2实例

    非泛型函数,查找以给定的String数组,若找到匹配的字符串,返回下标

    5.关联类型

    定义一个协议的时候,声明一个或多个关联类型作为协议定义的一部分是非常有用的,一个关联类型给定作用于协议部分的类型一个节点名。作用于关联类型上实际是不需要指定的,直到该协议接受。关联类型为 typealias关键字

    5.1实例

    定义ItemType关联类型,1. append方法添加一个新的item 2. count方法获取数量 3. 通过索引值检索到每一个item
    copy
    protocolContainer{
  • typealiasItemType
  • mutatingfuncappend(item:ItemType)
  • varcount:Int{get}
  • subscript(i:Int)->ItemType{get}
  • //IntStack的非泛型版本,实现Container协议的所有三个要求
  • structIntStack:Container{
  • //originalIntStackimplementation
  • //conformancetotheContainerprotocol
  • typealiasItemType=Int
  • mutatingfuncappend(item:Int){
  • self.push(item)
  • varcount:Int{
  • returnitems.count
  • subscript(i:Int)->Int{
  • returnitems[i]
  • //遵循Container协议的泛型版本
  • structStack<T>:Container{
  • //originalStack<T>implementation
  • //conformancetotheContainerprotocol
  • mutatingfuncappend(item:T){
  • subscript(i:Int)->T{
  • }

  • 5.2扩展一个存在的类型为已指定关联类型

    Swift中的Array已经提供了一个append方法一个count属性和通过下标查找元素的功能,都已满足Container协议的要求,就意味着可以扩展Array去遵循Container协议,只要通过简单声明Array适用于该协议就可以了

    6.Where语句

    where语句要求一个关联类型遵循一个特定的协议,或那个特定的类型参数和关联类型可以是相同的
    copy
    //定义allItemsMatch的泛型函数检查两个Container单例是否包含相同顺序的相同元素,如果匹配返回ture,否则返回false
  • funcallItemsMatch<
  • C1:Container,0); background-color:inherit">C2:Container
  • whereC1.ItemType==C2.ItemType,C1.ItemType:Equatable>
  • (someContainer:C1,0); background-color:inherit">anotherContainer:C2)->Bool{
  • //checkthatbothcontainerscontainthesamenumberofitems
  • ifsomeContainer.count!=anotherContainer.count{
  • returnfalse
  • //checkeachpairofitemstoseeiftheyareequivalent
  • foriin0..someContainer.count{
  • ifsomeContainer[i]!=anotherContainer[i]{
  • //allitemsmatch,soreturntrue
  • true
  • varstackOfStrings=Stack<String>()
  • stackOfStrings.push("uno")
  • stackOfStrings.push("dos")
  • stackOfStrings.push("tres")
  • vararrayOfStrings=["uno","dos","tres"]
  • ifallItemsMatch(stackOfStrings,arrayOfStrings){
  • println("Allitemsmatch.")
  • }else{
  • println("Notallitemsmatch.")
  • //prints"Allitemsmatch."

  • @H_502_0@FROM: http://blog.csdn.net/huangchentao/article/details/32718325

    大佬总结

    以上是大佬教程为你收集整理的Swift 泛型(十九)全部内容,希望文章能够帮你解决Swift 泛型(十九)所遇到的程序开发问题。

    如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

    本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
    如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
    标签: