swift泛型有哪些

本篇内容介绍了“swift泛型有哪些”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

成都创新互联公司从2013年成立,先为苏尼特右等服务建站,苏尼特右等地企业,进行企业商务咨询服务。为苏尼特右企业网站制作PC+手机+微官网三网同步一站式服务解决您的所有建站问题。

泛型类型约束

//泛型类型约束
func findIndex(of valueToFind: T, in array: [T]) -> Int? {
    for (index, value) in array.enumerated() {
        if value == valueToFind {
            return index
        }
    }
    return nil
}

print(findIndex(of: "abc", in: ["a", "b", "c", "abc"]))
//Optional(3)

关联类型:协议使用泛型

//关联类型:协议使用泛型
protocol Container {
    associatedtype ItemType: Equatable
    
    mutating func append(_ item: ItemType)
    
    var count: Int { get }
    
    subscript(i: Int) -> ItemType { get }
}

struct IntStack: Container {
    var items = [Int]()
    
    mutating func push(_ item: Int) {
        items.append(item)
    }
    
    mutating func pop() -> Int {
        return items.removeLast()
    }
    
    //指定类型
    typealias ItemType = Int
    
    mutating func append(_ item: Int) {
        self.push(item)
    }
    
    var count: Int {
        return items.count
    }
    
    subscript(i: Int) -> Int {
        return items[i]
    }
}

泛型结构体使用泛型协议

泛型结构体使用泛型协议,不需要用 typealias 指定类型,可以推断出来

//泛型结构体使用泛型协议,不需要用 typealias 指定类型,可以推断出来
struct Stack: Container {
    var items = [Element]()
    
    mutating func push(_ item: Element) {
        items.append(item)
    }
    
    mutating func pop() -> Element {
        return items.removeLast()
    }
    
    mutating func append(_ item: Element) {
        self.push(item)
    }
    
    var count: Int {
        return items.count
    }
    
    subscript(i: Int) -> Element {
        return items[i]
    }
}

var stack = Stack()
stack.push("abc")
print(stack.count)
//1

泛型 where 子句

//泛型 where 子句
func allItemMatch(_ someContainer: C1, _ anotherContainer: C2) -> Bool where C1.ItemType == C2.ItemType {
    if someContainer.count != anotherContainer.count {
        return false
    }
    
    for i in 0..

where扩展泛型

//where扩展泛型
extension Stack where Element: Comparable {
    
}

关联类型的泛型 where 子句

//关联类型的泛型 where 子句
protocol ContainerTwo {
    associatedtype ItemType: Equatable
    
    mutating func append(_ item: ItemType)
    
    var count: Int { get }
    
    subscript(i: Int) -> ItemType { get }
    
    associatedtype Iterator: IteratorProtocol where Iterator.Element == ItemType
    
    func makeIterator() -> Iterator
}

扩展泛型下标

//扩展泛型下标
extension ContainerTwo {
    subscript(indices: Indices) -> [ItemType] where Indices.Iterator.Element == Int {
        var result = [ItemType]()
        for index in indices {
            result.append(self[index])
        }
        return result
    }
}

“swift泛型有哪些”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注创新互联网站,小编将为大家输出更多高质量的实用文章!


分享题目:swift泛型有哪些
文章路径:http://bzwzjz.com/article/gdeshh.html

其他资讯

Copyright © 2007-2020 广东宝晨空调科技有限公司 All Rights Reserved 粤ICP备2022107769号
友情链接: 重庆企业网站建设 自适应网站建设 企业网站建设 品牌网站建设 成都网站设计 网站制作 成都网站制作 成都做网站建设公司 成都网站设计 成都网站建设公司 成都响应式网站建设 成都网站建设 网站建设公司 温江网站设计 手机网站制作 LED网站设计方案 网站设计公司 成都网站设计 响应式网站建设 定制网站建设 成都网站建设推广 成都网站建设公司