If you just want a simple ivar, instead of a property, you simply want to define it in your @implementation { ... }
.
There's simply no need for it to be in the @interface
as this exposes your class's inner workings to other classes. See this amazingly detailed answer for more info.
Example of ivar usage:
@implementation YourViewController {
Cube *cube;
}
You can then load this in from your viewDidLoad
:
- (void)viewDidLoad {
[super viewDidLoad];
currentSide = 0;
[self loadStage];
cube = [[Cube alloc] init];
}
And can freely access it throughout your class:
-(void) foo {
cube.bar = @"baz";
}
I prefer ivars over properties for something like this (internal variables) for a few reasons:
If you aren't using any getters or setters, why overlay an extra layer over the ivar if you're not going to use it? (that's all properties are at the end of the day).
They're much cleaner to use in your class (cube
instead of self.cube
).
They're much quicker to define.
They're much easier to distinguish from external properties (I like to keep external values and internal values separated).
They're faster. Okay, only by a couple of nanoseconds, but every little counts.
Although, it's worth noting that properties are better than ivars (for internal storage) in some situations:
If you want to make your code thread safe, you should be using atomic
properties in order to ensure that read & writes are serialised correctly.
If you want to use internal getters and setters (I have never needed to do this, but it's there if you want).
If you want to use KVO (although, again I've never needed to this internally).
At the end of the day, it's still a matter of preference. I am aware that I was maybe a little too sledge-hammer-y in asserting that ivars are better than properties in some of the comments I made.