溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

swift泛型有哪些

發(fā)布時間:2021-10-13 11:59:42 來源:億速云 閱讀:190 作者:iii 欄目:編程語言

本篇內容介紹了“swift泛型有哪些”的有關知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠學有所成!

泛型類型約束

//泛型類型約束
func findIndex<T: Equatable>(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)

關聯(lián)類型:協(xié)議使用泛型

//關聯(lián)類型:協(xié)議使用泛型
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]
    }
}

泛型結構體使用泛型協(xié)議

泛型結構體使用泛型協(xié)議,不需要用 typealias 指定類型,可以推斷出來

//泛型結構體使用泛型協(xié)議,不需要用 typealias 指定類型,可以推斷出來
struct Stack<Element: Equatable>: 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<String>()
stack.push("abc")
print(stack.count)
//1

泛型 where 子句

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

where擴展泛型

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

關聯(lián)類型的泛型 where 子句

//關聯(lián)類型的泛型 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: Sequence>(indices: Indices) -> [ItemType] where Indices.Iterator.Element == Int {
        var result = [ItemType]()
        for index in indices {
            result.append(self[index])
        }
        return result
    }
}

“swift泛型有哪些”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關的知識可以關注億速云網站,小編將為大家輸出更多高質量的實用文章!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經查實,將立刻刪除涉嫌侵權內容。

AI