类型 A 要求类型 B 是一个类类型 swift 4 [英] type A requires that type B be a class type swift 4
问题描述
下面的代码给了我一个编译错误
<块引用>'WeakReference' 要求 'ServiceDelegate' 是一个类类型
protocol ServiceDelegate: AnyObject {func doIt()}类 SomeClass() {//编译错误出现在这一行私有变量观察者 = [WeakReference]()}
弱引用代码:
final class WeakReference{私有(设置)弱变量值:T?初始化(值:T?){self.value = 价值}}
我该如何解决这个错误?应按照此网站正确声明委托.>
到目前为止我尝试过的:
- 将委托协议一致性从
AnyObject
更改为class
没有解决问题. - 在干净的操场上试试上面的代码.
你不能有 WeakReference
.ServiceDelegate
本身不是 AnyObject
,它只要求符合它的任何东西都是 AnyObject
.
您需要使 SomeClass
泛型并使用泛型类型作为 WeakReference
的类型:
class SomeClass{私有变量观察者 = [WeakReference<T>]()}
如果 SomeClass
上的泛型过于紧凑,并且您希望能够将多个不相关类的实例作为观察者,那么我会通过放弃 WeakReference上的泛型参数来实现代码>:
final class WeakServiceDelegate {私有(设置)弱变量值:ServiceDelegate?初始化(值:ServiceDelegate?){self.value = 价值}}类 SomeClass {私有变量观察者 = [WeakServiceDelegate]()}
或者,您可以使 WeakReference
有条件地符合 ServiceDelegate
:
extension WeakReference: ServiceDelegate where T: ServiceDelegate {func doIt() {价值?.doIt()}}
然后在SomeClass
中使用ServiceDelegate
的数组:
class SomeClass {私有变量观察者 = [ServiceDelegate]()func addObserver(_观察者:T){观察者.追加(弱参考(值:观察者))}}
the following code gives me a compile error
'WeakReference' requires that 'ServiceDelegate' be a class type
protocol ServiceDelegate: AnyObject {
func doIt()
}
class SomeClass() {
// compile error occurs in this line
private var observers = [WeakReference<ServiceDelegate>]()
}
WeakReference code:
final class WeakReference<T: AnyObject> {
private(set) weak var value: T?
init(value: T?) {
self.value = value
}
}
How can I fix this error? Delegate should be declared correctly as per this site.
What I have tried so far:
- Changing the delegate protocol conformance from
AnyObject
toclass
does not solve the problem. - Try the above code in a clean playground.
You can't have a WeakReference<ServiceDelegate>
. ServiceDelegate
itself is not an AnyObject
, it just requires that anything that conforms to it be an AnyObject
.
You would need to make SomeClass
generic and use the generic type as the type for the WeakReference
:
class SomeClass<T: ServiceDelegate> {
private var observers = [WeakReference<T>]()
}
If the generic on SomeClass
is too constricting and you want to be able to have instances of multiple unrelated classes as observers then I would do it by abandoning the generic parameter on WeakReference
:
final class WeakServiceDelegate {
private(set) weak var value: ServiceDelegate?
init(value: ServiceDelegate?) {
self.value = value
}
}
class SomeClass {
private var observers = [WeakServiceDelegate]()
}
Alternatively you could make WeakReference
conditionally conform to ServiceDelegate
:
extension WeakReference: ServiceDelegate where T: ServiceDelegate {
func doIt() {
value?.doIt()
}
}
And then use an array of ServiceDelegate
in SomeClass
:
class SomeClass {
private var observers = [ServiceDelegate]()
func addObserver<T: ServiceDelegate>(_ observer: T) {
observers.append(WeakReference(value: observer))
}
}
这篇关于类型 A 要求类型 B 是一个类类型 swift 4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!