xcode - Swift 属性 - 避免对 n 个类进行多个声明?

标签 xcode swift properties protocols

我在 Swift 中定义了以下协议(protocol):

protocol Condition {

var column : RepositoryTableColumn! {get set}
var sourceTable : RepositoryTable! {get set}
var conditionalValue : String! {get set}
var preventsClear : Bool! {get set}

func getCondition() -> String
func accept(visitor : ConditionVisitor)

}

协议(protocol)扩展了(目前)8 个类,这意味着每个类都需要有一个声明:

class IsEqual : Condition {

var column : RepositoryTableColumn!
var sourceTable : RepositoryTable!
var conditionalValue : String!
var preventsClear : Bool!

}

我的问题是,如何避免每个类中都有这四个声明?我试图避免这种情况的原因是我不想重复代码,即在本例中是声明。

对于构造函数,我创建了 Condition 类的扩展,但这不能暗示属性,因为扩展无法保存它们。

有什么建议吗?

最佳答案

如果您的属性可以计算而不是存储,您可以创建 Condition扩展,为采用您的协议(protocol)的任何内容提供默认实现。不幸的是,您无法将存储的属性添加到扩展中的类中,因此如果简洁对您来说很重要,您可能必须重新构建模型。

关于xcode - Swift 属性 - 避免对 n 个类进行多个声明?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34394315/

相关文章:

objective-c - Swift 如何在单行上编写语句

ios - AdMob 没有出现在我的 SpriteKit 游戏中

java - 用于构建可执行 jar 的通用 pom.xml : How can I specify dependency groupId/artifactId/version at runtime?

xcode 调试器选项

ios - 按主页按钮后返回同一 View

ios - 减小 IOS 中的应用程序大小

ios - 如何快速向 iOS 应用程序添加/更改语音性别?我正在使用 AVSpeechSynthesisvoice 类来获取语音

c# - 如何从对包含类型的引用中获取 ICollection<> 属性的名称?

JavaScript:如何修复未正确分配属性值的实例

ios - xcode 7.2 无法构建 Objective-C 模块