首页 > 解决方案 > typealias 泛型函数变量

问题描述

我在A 类中有以下函数声明

typealias Callback<T> = (Result<T>) -> ()

试图在B 类中声明变量

var callbackVariable: A.Callback<T>?

编译器说:使用未声明的类型“T”

如何在 B 类中声明变量?

标签: iosswiftfunctiongenericstype-alias

解决方案


您需要:

  1. 为 T 指定类型
  2. 或者,也将 B 类设为通用。
// 1. Specify a type for T
class B {
  var callbackVariable: A.Callback<String>? // Or some other type
}

// 2. Or, make the B class generic as well.
class B<T> {
  var callbackVariable: A.Callback<T>?
}

推荐阅读