In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces the relevant knowledge of "what are swift generics?". In the operation of actual cases, many people will encounter such a dilemma. Then let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Generic type constraint / / generic type constraint 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) Association type: protocol use generic / / association type: protocol use generic 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 ()} / / specified type typealias ItemType = Int mutating func append (_ item: Int) {self.push (item)} var count: Int {return items.count} subscript (I: Int)-> Int {return Items [I]}} generic structures use generic protocols
Generic structures use generic protocols and do not need to specify types with typealias, which can be inferred
/ / generic structures use generic protocols, and there is no need to specify types with typealias We can infer that struct Stack: Container {var items = [Element] () mutating func push (_ item: Element) {items.append (item)} mutating func pop ()-> Element {return items.removeLast ()} mutating func append (_ self.push: 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 generic where clause / / generic where clause func allItemMatch (_ generic: C1) _ anotherContainer: C2)-> Bool where C1.ItemType = = C2.ItemType {if someContainer.count! = anotherContainer.count {return false} for i in 0.. ItemType {get} associatedtype Iterator: IteratorProtocol where Iterator.Element = = ItemType func makeIterator ()-> Iterator} extended generic subscript / / extended generic subscript extension ContainerTwo {subscript (indices: index)-> [ItemType] where Indices.Iterator.Element = = Int {var result = [ItemType] () for index in indices {result.append (self [index])} return result} } this is the end of the introduction to "what are swift generics"? Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.