Swift 如何使用共享实例变量分解符合多个接口的类?
我现在怎样才能把这门长课分解呢?我在考虑使用扩展,比如Swift 如何使用共享实例变量分解符合多个接口的类?,swift,swift-extensions,Swift,Swift Extensions,我现在怎样才能把这门长课分解呢?我在考虑使用扩展,比如 class X: Interface1, Delegate1, Delegate2 { var sharedVariable = true .... very long code comes here, that implements the delegates ... } 这种方法的问题是,由于两个扩展都使用sharedVariable,所以将其分解为扩展没有多大用处。。。 还是这样?我还能用它做什么呢?这取决于代码的其
class X: Interface1, Delegate1, Delegate2 {
var sharedVariable = true
.... very long code comes here, that implements the delegates ...
}
这种方法的问题是,由于两个扩展都使用sharedVariable,所以将其分解为扩展没有多大用处。。。
还是这样?我还能用它做什么呢?这取决于代码的其余部分是如何构造的。将代码分解为扩展的一个很好的理由是在适当的范围和/或上下文中提供每个扩展。在极端情况下,可以将类型实现为数据,并根据各种上下文和范围中所需的观点,提供所有函数和计算属性作为扩展
extension X: Delegate1 {
// .. some code here, uses sharedVariable
}
extension X:Delegate2 {
// and some here, also uses sharedVariable
}