【发布时间】:2016-06-03 15:22:02
【问题描述】:
我有一个方法:
public func someMethod<Controller>(controller: Controller) {
print(controller)
typealias HandlerType = Controller -> UIViewController
let handler: HandlerType
print(handler.dynamicType)
}
在使用 UIViewController 的子实例(在我的例子中是 ViewController)调用它之后,它会打印:
<Test.ViewController: 0x7fc43be479d0>
UIViewController -> UIViewController
我的目标是拥有这样的类型别名:
Test.ViewController -> UIViewController
我有 typealias 和声明的 valriable:
public typealias ControllerType = UIViewController
public private(set) static var visibleController: ControllerType?
而方法调用是:
if let controller = visibleController {
someMethod(controller)
}
我不明白为什么 ViewController 在 typealias 中变成 UIViewController,所以如果有人遇到此类问题和/或有解决方案,请告诉我。
提前谢谢你。
【问题讨论】:
-
那是你的完整代码吗?在我看来,这会发生在诸如
Controller: UIViewController之类的通用约束下。 -
其实这个例子没关系。对于我的代码,就像您说的那样,我只是更改了它以确保即使在这种情况下它也不起作用。
-
您遇到了类型系统问题。即使您认为这无关紧要,我也会鼓励您准确了解示例中的类型。
标签: swift generics type-alias