zoukankan      html  css  js  c++  java
  • Swift泛型协议的N种用法

      They said "you should learn a new language every year," so I  learned Swift. Now  I  learn a new language every two weeks!

      这个笑话绝对是我看过的Swift被黑的最惨的一次!所以今天我们来学习一下Swift的泛型。

      Swift的泛型有点奇怪,针对Class和Function,都是通过<Type>来定义,和C#一摸一样,同样也有where关键字进行约束。

    func swapTwoValues<T>(inout a: T, inout _ b: T) {
        let temporaryA = a
        a = b
        b = temporaryA
    }
    
    class CanPrintBase<T> {
        func PrintType(output: T) -> Void {}
    }
    

      但是面对Interface也就是Swift里的Protocol,需要使用associatedtype关键字来定义泛型:

    protocol CanPrint {
        
        associatedtype E
        
        func PrintType(output: E) -> Void
    }
    

      那要怎么去实现这个接口呢?通常是这样子的:

    class TypePrinter0 : CanPrint{
        
        typealias E = String
        
        func PrintType(output: E) {
            print(type(of:output))
        }
    }
    
    let print0 = TypePrinter0()
    print0.PrintType(output: "String Type")
    

      然后就会在output窗口打印“String”

      阿西吧!这么奇怪的语法简直不能忍!就不能用<Type>来写吗?

      曲线救国的话,我们可以考虑模拟一个抽象类class CanPrintBase<T>,通过继承来实现同样的效果:

    class TypePrinter3: CanPrintBase<String>{
        
        override func PrintType(output: String){
             print(type(of:output))
        }
    }
    
    let print3 = TypePrinter3()
    print3.PrintType(output: "String Type")
    

      那么我们像C#一样直接在类定义的时候通过占位符的方式可以嘛?

    //This one cannot work!
    class TypePrinter1<E: String> : CanPrint{
       
        func PrintType(output: E) {
            print(output)
        }
    }
    

      错误提示为:Inheritance from non-protocol, non-class type 'String'。也就是说如果是class类型的话就可以:

    public class SomeType {}
    
    class TypePrinter2<E: SomeType> : CanPrint{
    
        func PrintType(output: E) {
            print(output)
        }
    
    }
    
    let print2 = TypePrinter2()
    print2.PrintType(output: SomeType())
    

      反之我们也可以写成这样

    class TypePrinter5 : CanPrint{
        
        typealias E = SomeType
        
        func PrintType(output: E) {
            print(output)
        }
    }
    
    let print5 = TypePrinter5();
    print(type(of: print5))
    print(type(of: print2))
    

      将类型打印出来的话,分别是TypePrinter5和TypePrinter2<SomeType>,也就是说这两种写法得到的类型是完全不一样的。

      呵呵也是蛮妖的嘛,还可以把类型的具体定义留到使用时再声明:

    class TypePrinter4<E> : CanPrint{
        
        func PrintType(output: E) {
            print(output)
        }
    }
    let print4 = TypePrinter4<SomeType>()
    print4.PrintType(output: SomeType())
    
    let print6 = TypePrinter4<String>()
    print6.PrintType(output: "I am a String")
    

      这一点又和C#傻傻分不清楚了。

      本篇实在是蛮无聊的纠缠与Swift泛型协议的语法,如孔乙己般尝试了回字的N种写法。至于为什么Swift要这么设计,我们下一篇可以尝试和C#对比看看。

      GitHub:

      https://github.com/manupstairs/LearnSwift/tree/master/GenericProtocolTest

  • 相关阅读:
    #什么是spring#
    spark sql 判断一列是否包含某字符
    win10 安装微软商店
    python获取一段时间的日期
    css 网格
    css 网格属性总结
    css flex容器属性总结
    CSS Flexbox
    响应式Web设计
    css 伪类选择器
  • 原文地址:https://www.cnblogs.com/manupstairs/p/5960992.html
Copyright © 2011-2022 走看看