I\'m trying to mark a variable of a generic type as weak:
class X {
weak var t: T?
}
If I don\'t put in any constraints for
You want AnyObject
, which the Swift docs describe as:
The protocol to which all classes implicitly conform.
class X<T: AnyObject> {
weak var t: T?
}
class C { }
let x = X<C>() // works fine
x.t = C()
// error: type 'Int' does not conform to protocol ‘AnyObject’
// (because Int is a struct)
let y = X<Int>()