I think I've been using Objective-C properties incorrectly. Specifically, I've been treating them like instance variables.
Here's an example of a recent interface:
// AIClass.h
#import "AIDataUtils.h"
@interface AIViewController : UIViewController
@property (strong, nonatomic) AIDataUtils *dataUtils;
@end
Then, in my implementation, I would use self.dataUtils
as a way for any method in the class to easily access the same thing. No object from the outside would ever be interacting with that property.
What I'm realizing is that what I should have been doing is importing and declaring AIDataUtils
in the implementation and not the interface. I think that would look like this:
// AIClass.m
#import "AIDataUtils.h"
@interface AIViewController ()
{
AIDataUtils *dataUtils;
}
@end
@implementation AIViewController
- (void)viewDidLoad
{
[super viewDidLoad];
dataUtils = [[AIDataUtils alloc] init];
...
}
The docs say:
Avoid explicitly declaring public instance variables. Developers should concern themselves with an object’s interface, not with the details of how it stores its data.
My understanding here is if another object has no business touching AIDataUtils
, don't put it in the interface. The fact that a property exists in an interface should be a hint that you're supposed to feed or do something with that property.
Am I hot or cold?