【发布时间】:2015-08-21 10:43:29
【问题描述】:
所以我正在考虑在我的项目中使用自定义模式,但我无法让它发挥作用。主要思想是更改每个子类上的typealias 以访问子类特定接口。
protocol InstanceInterface: class {
typealias Interface
var interface: Interface { get }
}
// Baseclass
protocol FirstClassInterface: class { /* nothing here for the example */ }
class FirstClass: InstanceInterface, FirstClassInterface {
typealias Interface = FirstClassInterface
var interface: Interface { return self }
}
// Subclass
protocol SecondClassInterface: FirstClassInterface {
func foo()
}
class SecondClass: FirstClass, SecondClassInterface {
typealias Interface = SecondClassInterface // <--- This does nothing :(
func foo() { print("hello world") } // Swift 2.0 here
}
// Lets say I want to call foo trough the interface
let test = SecondClass()
test.interface.foo() // 'Interface' does not have a member named 'foo'
是我做错了什么还是我误解了一些 Swift 概念?!我确实需要在这里进行子类化,以免一遍又一遍地实现超类协议中的所有内容。我的小模式甚至可能吗?我会很感激任何帮助。 :)
【问题讨论】:
标签: swift generics protocols swift2