To avoid capturing of 'self' in a block, we often use a weak pointer. However, for some 'non-property' ivars (otherValue in this example):
@implementation MyClass
{
NSString *otherValue;
}
We need a strong ref and an arrow accessor (->) usually done like:
MyClass *blockself = weakself;
blockself->otherValue
The problem is, accessing blockself->otherValue will crash if blockself is nil.
I can check for if (blockself), but it 'can' be release right after, in the next line.
Example:
__weak MyClass *weakself = self;
[serverManager runSomething:^(Result *res) {
// If pointer (weakself) is released, no problem. Accessing though a 'property'
if (weakself.value) {
}
// For local iVars though:
MyClass *blockself = weakself;
// This will crash if 'blockself' is nil
if (blockself->otherValue) {
}
// Can test for nil here
if (blockself) {
// But..
// Potentiall, the pointer 'could' be released here
if (blockself->_value) {
}
}
So, is the only safer way is to migrate from iVars to use a @property? Is there a 'safe' way to access iVars thought an arrow (->) accessor?