Swift 2.2:从类协议访问/修改类变量

时间:2016-04-02 03:02:25

标签: ios objective-c xcode swift uiview

我一直在挖掘,似乎无法找到答案......我正在努力实现类似下面的简化协议设置。

//PROTOCOL SETUP
protocol myProtocol: class {
   var thisVariable: CustomType? {get set}
}

extension myProtocol {
   func doSomeThings() { //IMPLEMENTATION SPOT 1// }
}

extension theClassIAmUsing: myProtocol {
   func doSomeThings() { //IMPLEMENTATION SPOT 2// }
}

//NEW IN SWFIT
extension myProtocol where Self: theClassIAmUsing {
   func doSomeThings() { //IMPLEMENTATION SPOT 3// }
} 

//CLASS:
class theClassIAmUsing: UIView, myProtocol {
   var thisVariable: CustomType? = CustomType() //etc...
   //does not implement doSomeThings() but is available

   init() {
   //some init
   }
}

我尝试使用的实现是基于以下内容执行逻辑/功能:

self.thisVariable!.element

IT工作!,但仅限于实施第2点和第3点 ......

我的问题是,如果我在我的应用程序中使用UIView进行子类化,我将需要添加myProtocol的扩展名“self:”是(下一个自定义类)每个时间我创建了一个新的子类.......(是的,这清理了类定义代码部分,但似乎很乏味。)

这可能是挑剔的,因为能够在协议的扩展中访问self.thisVariable似乎是不合理的,以便它可以在我所有的子类UIViews中使用?

我错过了什么吗?

我在IMPLEMENTATION SPOT 1中得到的错误是:

warning: could not load any Objective-C class information. 
This will significantly reduce the quality of type information available.

0 个答案:

没有答案