Can anyone explain to me what this error means and what is wrong with this attempted implementation of a Weak wrapper in Swift.
Just copy this into a new 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 '_?'
I get the errors:
error: 'MyProto' is not convertible to 'AnyObject'
error: cannot convert value of type 'MyProto' to expected argument type '_?'