美文网首页
Swift - Generic 泛型

Swift - Generic 泛型

作者: tom__zhu | 来源:发表于2023-08-18 11:02 被阅读0次

Generic
编写适用于多种类型的代码,并为这些类型指定需求。
泛型是Swift最强大的特性之一,Swift标准库的大部分都是用泛型代码构建的。

作用对象:

  1. 方法入参/属性声明
  2. class/struct
  3. protocol
// Make Generic Type with Class
class Node<ElementType> {
    let value: ElementType
    var next: Node?
    init(value: ElementType, next: Node?) {
        self.value = value
        self.next = next
    }
}
// Make Generic Type with function
extension Node {
    func getValue() -> ElementType {
        return self.value
    }
    
    @discardableResult
    func description() -> [ElementType] {
        var results: [ElementType] = []
        var head: Node? = self
        while let currentNode = head {
            results.append(currentNode.value)
            head = currentNode.next
            print(currentNode.value)
        }
        return results
    }
}
// Make Generic Type with Protocol
protocol Mathmatic {
    // Generic with associatedtype in Protocol
    associatedtype ElementType
    func sum(lhs: ElementType, rhs: ElementType) -> ElementType
}

class IndexClass: Mathmatic {
    // Generic with typealias in class
    typealias ElementType = Int
    func sum(lhs: Int, rhs: Int) -> Int {
        return lhs + rhs
    }
}

class MoneyClass: Mathmatic {
    // Generic with typealias in class
    typealias ElementType = Double
    func sum(lhs: Double, rhs: Double) -> Double {
        return lhs + rhs
    }
}

Generic 约束

  1. Inherit
  2. where

Inherit

protocol Mathmatic {
    // Generic with associatedtype in Protocol
    associatedtype ElementType: Numeric
    func sum(lhs: ElementType, rhs: ElementType) -> ElementType
}

where

protocol Mathmatic {
    // Generic with associatedtype in Protocol
    associatedtype ElementType
    func sum(lhs: ElementType, rhs: ElementType) -> ElementType
}

// Only add typeCheck for StringProtocol
extension Mathmatic where ElementType: StringProtocol {
    func typeCheck() {
        print("Error with string type")
    }
}

class MoneyClass: Mathmatic {
    // Generic with typealias in class
    typealias ElementType = Double
    func sum(lhs: Double, rhs: Double) -> Double {
        self.typeCheck() // Build Error: "Referencing instance method 'typeCheck()' on 'Mathmatic' requires that 'MoneyClass.ElementType' (aka 'Double') conform to 'StringProtocol'"
        return lhs + rhs
    }
}

class NameClass: Mathmatic {
    typealias ElementType = String
    func sum(lhs: String, rhs: String) -> String {
        self.typeCheck()  // Build Success
        return lhs + rhs
    }
}

https://medium.com/globant/swift-generics-and-associated-types-73aa2b184c7a#:~:text=Swift%20Generics%20and%20Associated%20Types%201%20Generics%20syntax,generally%20start%20implementation%20by%20adding%20protocols%20first.%20

相关文章

  • Swift进阶之泛型

    泛型Generic在swift中非常重要,它提升了代码的通用性和简洁性,很多开源的组件都是通过泛型来实现。泛型是什...

  • Swift:Deep in Generic Swift 深入泛型

    泛型的意义 将通用算法和具体数据类型分离 为什么要使用泛型? 从上面的意义可以看出,使你的代码逻辑划分更清楚,算法...

  • Swift - 进阶之泛型编程

    Swift语言有很多强大的特性,泛型编程(generic programming)就是其中之一,我们也可以将其简称...

  • Java 中的泛型 (Generic)

    泛型 (Generic) 泛型 (Generic),即“参数化类型”,就是允许在定义类、接口、方法时使用类型形参,...

  • Generic泛型

    泛型:JDK1.5版本以后出现的新特性,用于解决安全问题,是一个类型安全机制。 好处:1.将运行时期出现问题Cla...

  • 泛型Generic

    用二位坐标定义一个平面上的点a(x,y): 精度不够,提高精度需要重新定义高精度的类: 上面定义的两个类的代码非常...

  • 泛型(Generic)

    泛型:JDK1.5版本以后出现的新特性,用于解决安全问题,是一个类型安全机制。 好处:1.将运行时期出现问题Cla...

  • 泛型Generic

    a. 在java泛型中,如果创建一个运用泛型的数组,完整的写法为: 即无法直接创建,只能创建Object类型,然后...

  • Generic泛型

    网址 https://www.cnblogs.com/dotnet261010/p/9034594.html De...

  • 泛型generic

    先看一段代码 上边的join方法的参数,有3种情况,都可以运行成功。 但是,当我们提出了新的需求,比如当first...

网友评论

      本文标题:Swift - Generic 泛型

      本文链接:https://www.haomeiwen.com/subject/rofdmdtx.html