automatic ref counting - Type and declaration restrictions on Swift capture specifiers -
i'm confused type requirements , declaration restrictions swift capture specifiers. documentation says weak
references must var
, "of optional type", , unowned
references must of non-optional type. apple's own api reference has examples of unowned(unsafe)
references declared optionals, while xcode interface builder creates weak
outlets , actions implicitly unwrapped (which not "of optional type" in language reference).
what types required each of swift capture specifiers? must var
, can let
?
fwiw, think its
weak
can type?
or type!
, not type
; , must var
unowned(safe)
(same unowned
) must type
; , may let
unowned(unsafe)
can type?
or type!
; , may let
but i'm far sure.
first cocoaor cocoa touch apis imported in swift from
objective-c`. mapped in swift best possible ways.so both can interoperable.
those unowned(unsafe)
properties marked assign
in objc. these unsafe
, arc doesn't retain object, , doesn't create sure reference gets reset nil if object deallocated. these old apple api , not changed arc , remain as
assignbut should make
delegateweak` not headers best practices in swift have made many tricks create swift , objective-c interoperable , if headers loose safety swift proveide.
you right
weak should optional
contain nil
automatically if objects deallocate , no other pointer retain
unowned(safe) should not optional
, not reset nil
if objects deal-located
unowned(unsafe) can or can not optional
not provide safety arc object deal-location , not reset nil
.you should not utilize in swift program.use unowned
if needed.it interoperability.
weak var
because can changed when objects deallocate , set nil
.
unowned(safe) , unowned(unsafe) can both var
or let
dependent on , run-time not alter these variables.
note:you can not declare unowned(unsafe) optional
in swift program.its interoperability , should not used in swift.they have made because of assign
or unretain
properties can nil
swift automatic-ref-counting optional
No comments:
Post a Comment