任何人都可以向我解释这个错误意味着什么,以及在Swift中尝试实现弱包装器有什么问题。
只需将其复制到新的Playground:
import Cocoa
public struct Weak<T> where T: AnyObject {
weak var value: T?
init (_ value: T?) {
self.value = value
}
}
public protocol MyProto: class {
}
public class Foo: MyProto {
}
var foos = [Weak<MyProto>]()
let foo = Foo()
for foo in foos {
if let bar = foo.value { // 'MyProto' is not convertible to 'AnyObject'
}
}
foos.append(Weak(foo as MyProto)) // Cannot convert value of type 'MyProto' to expected argument type '_?'
我收到了错误:
错误:&#39; MyProto&#39;不能转换为&#39; AnyObject&#39;
错误:无法转换类型&#39; MyProto&#39;的值预期参数类型&#39; _?&#39;