swift - weak cannot be applied to non-class type uiimageview -
i have class in swift needs have weak pointer array of objects allocated in class. have
class myview: uiview { var lines:[cashapelayer] = [] weak var avatars : [uiimageview]?
the error is
'weak' cannot applied non-class type '[uiimageview]'
i tried no avail:
weak var avatars : [uiimageview?]?
weak cannot applied non-class type:
it means can’t have weak reference value type instance(e.g. array, dictionary, string, etc…) because these struct not class. give weak reference of class-type(e.g uiimage, uiimageview, etc…).in case, trying give weak reference uiimageview array , know array value type, not possible.
for example:
weak var str: string? //compiletime error(wrong) weak var arr: array? //compiletime error(wrong) weak var imageview: uiimageview? //correct
in case of protocol: if have protocol of struct type:
protocol someprotocol{ func dosomething() }
we cannot declare variables of type weak:
weak var delegate: someprotocol? //compiletime error(wrong)
but if make protocol of class type this:
protocol someprotocol: class{ func dosomething() }
we can declare variables of type weak:
weak var delegate: someprotocol? //correct
i think understand it, why happens in protocol?
same reason: give weak reference of class-type
Comments
Post a Comment