Objective-C Quickies

From macwrench
Jump to navigation Jump to search


Encapsulating Data

Difference between __weak and __block

From the Apple Documentation:

__weak specifies a reference that does not keep the referenced object alive. A weak reference is set to nil when there are no __strong references to the object. __weak is the opposite of __strong.
__block variables live in storage that is shared between the lexical scope of the variable and all blocks and block copies declared or created within the variable’s lexical scope. Thus, the storage will survive the destruction of the stack frame if any copies of the blocks declared within the frame survive beyond the end of the frame (for example, by being enqueued somewhere for later execution). Multiple blocks in a given lexical scope can simultaneously use a shared variable.
Note: A variable maintains a __strong reference to an object only as long as that variable is in scope, or until it is reassigned to another object or nil. [...] You don’t need to specify __strong explicitly, because it is the default.

See also