I can define a class with a property to access my ivars from outside of the class.
I can also just use the myInst->ivar syntax to access in a C-struct fashion.
In C++, I would go with accessors, but in objective-c there are some cases where I may want direct access. The messaging system incurs a large performance hit with the accessor that matters in some contexts, since the message isn't inlined like it can be with C++ methods.
For example in a class that has an ivar called scalar and a property defined on it. scalar is a simple float value:
-(void) doWorkWithMyClass:(MyClass*)myinst
{
// array and other locals declaration omitted
for (int i = 0; i < reallyBigX; i++) {
result += [myinst scalar] * array[i];
[myinst computeNextScalar:i];
}
}
If i change [myinst scalar] to myinst->scalar, the method will run a lot faster, since using calls with the accessor would take up most of the CPU in this loop.
As with C++ I understand direct ivar access is in general discouraged, but in this context, when speed matters, is it acceptable? If not, is there a more preferred method that still uses an objective-c class?