I have a lot of methods that repeat this simple boilerplate:
- (id)myObject {
if(!_myObject) {
self.myObject = [_myObject.class new];
}
return _myObject;
}
So I want to replace this with a simple macro:
#define default_init(instance) \
if(!instance) instance = [instance.class new]; \
return instance;
So that I would only have to call:
- (id)myObject {
default_init(_myObject);
}
The above code currently compiles, but the issue is that the macro directly sets the instance variable's value. Instead, I'd like to call self.instance = value;
So instead of
if(!instance) instance = [instance.class new];
I'd like something like;
if(!instance) self.instance = [instance.class new];
But obviously the current code does not allow for this. How might I accomplish something like this?