Swift: How to define a UIView delegate with unowned(unsafe) reference? -
i find next code in uitableview class,
unowned(unsafe) var delegate: uitableviewdelegate? so wander how define unowned(unsafe) reference delegate uiview, encounter next error when write unowned(unsafe) keyword in class,
/users/larryhou/documents/xcode/atomicelements/atomicelements/atomicelementview.swift:32:25: 'unowned' cannot applied non-class type 'atomicelementviewdelegate?' protocol atomicelementviewdelegate:nsobjectprotocol { func didtap(target:atomicelementview, sender:uitapgesturerecognizer) } i can utilize weak keyword, want maintain reference until uiview deallocated.
those 2 designations - unowned , weak - equivalent perspective of references. differ presumption of existence - in swift, unowned instances presumed always exist long reference them does, whereas weak instances optionals - may exist, or may not, need utilize optional chaining or other means of working them.
in uikit, delegate pattern uiview subclasses declare them weak because delegate view controller owns view subclass subview of. declaring delegate referenced in situation set reference cycle, hence delegates typically declared weak keyword. if have different situation, can allow delegate referenced leaving out weak keyword.
in particular case, unowned (unsafe) artifact of beingness bridged objc.
swift delegates
No comments:
Post a Comment