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

概述

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
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为占位类型名字,可@L_772_4@多个类型参数,命名在尖括号中,用逗号分开
func swapTwoValues<T>(inout a: T,inout b: T) {
    let temporaryA = a
    a = b
    b = temporaryA
}

var someInt = 3
var anotherInt = 107
swapTwoValues(&someInt,b: &anotherint)

var someString = "Hello"
var anotherString = "world"
swapTwoValues(&someString,b: &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")
// 现在栈已经有4个String了
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 array.enumerate() {
        if value == valueToFind {
            return index
        }
    }
    return nil
}
let Strings = ["cat","dog","llama","parakeet","terrapin"]
if let foundIndex = findStringIndex(Strings,valueToFind: "llama") {
    print("The index of llama is \(foundIndeX)")
}
// 输出 "The index of llama is 2"


//Swift 标准库中定义了一个Equatable协议,该协议要求任何遵循的类型实现等式符(==)和不等符(!=)对任何两个该类型进行比较。所有的 Swift 标准类型自动@L_772_4@Equatable协议。
func findIndex<T: Equatable>(array: [T],valueToFind: T) -> Int? {
    for (index,value) in array.enumerate() {
        if value == valueToFind {   //不是所有的 Swift 中的类型都可以用等式符(==)进行比较
            return index
        }
    }
    return nil
}

let doubleIndex = findIndex([3.14159,0.1,0.25],valueToFind: 9.3)
// doubleIndex is an optional Int with no value,because 9.3 is not in the array
let StringIndex = findIndex(["Mike","Malcolm","Andrea"],valueToFind: "Andrea")
// StringIndex is an optional Int containing a value of 2

大佬总结

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

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

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