This isn't the right way to think about it. You want to modify an object, which has a property. So you capture the object in the block, and call its accessors (setFoo:
) to modify the property.
EDIT: From the various edits, you may be confusing how ObjC objects work and how C++ works. ObjC only uses pointers to objects, seldom copies them, and lacks the concept of a const
object (there are immutable objects, but it's just because they don't have mutators; you can't const
a mutable object like you can in C++).
__block
just means "this variable (not the object pointed to by it; this actual variable) should be passed by reference into the block rather than by value)." So when I say:
__block id foo;
this means that the foo
pointer itself can be changed. It has nothing at all to do with whether the object pointed to by foo
can be mutated. This makes no sense for a global or an ivar. ivars are implicitly struct fields. When you say _ivar
inside a block, the compiler implicitly converts this to self->_ivar
and then captures self
. It does not capture _ivar
(since that's just an offset into the self
struct). It's better to do the same using accessors, since it's much more explicit what you're doing, and you can use __weak
that way.