2016-03-02 54 views
6

我想写一个弱性能需求的协议。符合它的类必须能够为该属性指定任何类型。另外我不想指定实际的类型,所以它应该是用某种协议指定的类型。此代码显示了我对非弱财产的想法:Swift协议中关联类型的弱性要求

protocol ObjectProtocol: class { 
    typealias PropertyType 
    var property: PropertyType {get set} 
} 

protocol FirstPropertyProtocol: class {} 
protocol SecondPropertyProtocol: class {} 

class FirstObjectImpl: ObjectProtocol { 
    var property: FirstPropertyProtocol? 
} 

class SecondObjectImpl: ObjectProtocol { 
    var property: SecondPropertyProtocol? 
} 

它按预期工作。

我想弱财产做同样的:

protocol ObjectProtocol: class { 
    typealias WeakPropertyType: AnyObject //must be a class type 
    weak var weakProperty: WeakPropertyType? {get set} 
} 

protocol WeakPropertyProtocol: class {} 

class ObjectImpl: ObjectProtocol { 
    weak var weakProperty: WeakPropertyProtocol? 
} 

而且我得到了一个编译器错误:

Type 'ObjectImpl' does not conform to protocol 'ObjectProtocol'

有没有什么办法可以使这项工作?

+0

的问题是,协议不符合本身,而不是它从继承的任何协议,见http://stackoverflow.com/questions/33112559/protocol对于类似的问题,不符合自己。在你的情况下,WeakPropertyProtocol类型不符合AnyObject。 –

+0

感谢您的评论,马丁。有没有其他的方式来指定我的'WeakPropertyType'是没有这个'AnyObject'约束的类类型? –

+0

协议当前不需要将属性实现为弱存储属性。 https://stackoverflow.com/questions/47699813/weak-property-in-a-swift-protocol – Adobels

回答

3

我不相信一个协议可以强制执行弱点。例如:

protocol ObjectProtocol: class { 
    weak var weakProperty: AnyObject? {get set} 
} 

class ObjectImpl1: ObjectProtocol { 
    weak var weakProperty: AnyObject? 
} 

class ObjectImpl2: ObjectProtocol { 
    var weakProperty: AnyObject? 
} 

这些都编译好了,即使协议具有weak但ObjectImpl2没有实现它。

编辑:这是你以后在做什么...

protocol ObjectProtocol: class { 
    typealias WeakPropertyType: Any //must be a class type 
    var weakProperty: WeakPropertyType? {get set} 
} 

protocol WeakPropertyProtocol: class {} 

class ObjectImpl: ObjectProtocol { 
    typealias WeakPropertyType = WeakPropertyProtocol 
    weak var weakProperty: WeakPropertyProtocol? 
} 

实现需要使用任何而非AnyObject,因为WeakPropertyProtocol是一个协议,而不是一类?

或那样吗?...

protocol WeakPropertyProtocol: class {} 

protocol ObjectProtocol: class { 
    typealias WeakPropertyType: AnyObject //must be a class type 
    var weakProperty: WeakPropertyType? {get set} 
} 

class MyWeakClass: WeakPropertyProtocol { 

} 

class ObjectImpl: ObjectProtocol { 
    typealias WeakPropertyType = MyWeakClass 
    weak var weakProperty: MyWeakClass? 
} 

无论哪种方式,我认为关键是在定义类/协议以用于WeakPropertyType其中。

+0

这很有趣,但我不能接受你的答案。我并不需要强制执行弱点。但是我需要能够在符合我的协议的类中声明这个属性很弱。只有拥有类类型的属性可能很弱,并且我找不到如何在没有AnyObject约束的情况下指定它。 –

+0

我误解了你的问题。我会更新我的答案。 – Michael

+0

谢谢Michael!第二个是我需要的。它工作的很好,但我担心'ObjectProtocol'中的'weakProperty'没有用'weak'引用指定。此外,我需要在类实现中为'WeakPropertyType'别名明确指定一个类型。无论如何,您的解决方案目前最适合我。 –

2

我把它与@objc属性工作了WeakPropertyProtocol:

protocol ObjectProtocol: class { 
    typealias WeakPropertyType: AnyObject //must be a class type 
    weak var weakProperty: WeakPropertyType? {get set} 
} 

@objc protocol WeakPropertyProtocol {} 

class SomeObjectImpl: ObjectProtocol { 
    weak var weakProperty: WeakPropertyProtocol? 
} 

,因为我关注这个便条apple doc

Note also that @objc protocols can be adopted only by classes that inherit from Objective-C classes or other @objc classes.

我可以用这个限制住这不是一个最好的解决方案,但我会欣赏任何更好的解决方案

0

Swift 4版本。
我需要我的视图模型符合协议。它们不能保持协调对象:

protocol ViewModelType { 
    associatedtype CoordinatorType: AnyObject 
    weak var coordinator: CoordinatorType? { get } 
} 
+1

协议当前不需要将属性实现为弱存储属性。 https://stackoverflow.com/questions/47699813/weak-property-in-a-swift-protocol – Adobels