If a variable:
- Is declared in a class using ARC.
- Is used solely for class implementation (not exposed as part of the class interface).
- Does not require any KVO.
- Does not require any custom getter/setter.
Then it is appropriate to declare it as an ivar without a corresponding @property
/@synthesize
, and to refer to it directly within the implementation. It is inline with Encapsulation to declare this ivar in the class implementation file.
// MyClass.h
@interface MyClass : ParentClass
@end
// MyClass.m
@implementation MyClass {
NSString *myString;
}
- (void)myMethod {
myString = @"I'm setting my ivar directly";
}
@end
- This ivar will be treated as
__strong
by the ARC compiler. - It will be initialized to
nil
if it is an object, or0
if it is a primitive.