代码之家  ›  专栏  ›  技术社区  ›  Gintas_

如何将函数的参数强制转换为作为方法参数的函数中的任何参数?

  •  -1
  • Gintas_  · 技术社区  · 6 年前

    如何将函数的参数强制转换为作为方法参数的函数中的任何参数?

    struct InitializationStaticData: Decodable {}
    
    func method1(responseListener: @escaping (_ status:Int, _ data: InitializationStaticData?) -> Void)
    {
        let dsa = responseListener as! (Int, Any?) -> Void // EXC_BREAKPOINT
        let asd = responseListener as! (Int, Decodable?) -> Void // EXC_BREAKPOINT
        method2(responseListener: sdd)
    }
    

    我试着这样做:

    func method2<T>(responseListener: @escaping (_ status: Int, _ data: Any?) -> Void)
    {
    }
    
    1 回复  |  直到 6 年前
        1
  •  2
  •   vadian    6 年前

    基本上,一般方法是正确的。

    更改的类型 data T 并将泛型约束为 Decodable

    func method2<T : Decodable>(responseListener: @escaping (Int, T) -> Void)
    {
    }
    

    下划线字符和参数标签是Swift 2遗留的,在Swift 3中不使用它们。+