For a custom (lazy) getter method, you need to access the instance variable directly (regardless of whether you use ARC or not). Therefore, you must synthesize the property as
@synthesize timeFormatter = _timeFormatter;
Then your getter method
- (NSDateFormatter *)timeFormatter { if (_timeFormatter == nil) { _timeFormatter = [[NSDateFormatter alloc] init]; [_timeFormatter setDateFormat:@"h:mm a"]; } return _timeFormatter; }
You only need to add a synchronization mechanism if the resource is accessed simultaneously from several threads, which also does not depend on ARC or not.
(Note: newer versions of Xcode can automatically create the @synthesize operator and use the underscore prefix for instance variables. In this case, however, since the property is read-only and you provide the getter method, Xcode does not automatically synthesize the property.)
ADDED: The following is a complete code example for your convenience:
MyClass.h:
MyClass.m:
ADDITIONAL INFORMATION . In fact, your pre-ARC timeFormatter getter method works unchanged also with ARC if the property is synthesized as
@synthesize timeFormatter; // or: @synthesize timeFormatter = timeFormatter;
The only โmistakeโ you made was to replace timeFormatter with self.timeFormatter inside the getter method. This creates two problems:
- Reading
self.timeFormatter inside the getter method results in infinite recursion. - Setting
self.timeFormatter not allowed due to a read-only attribute.
So, if you just leave the getter timeFormatter method as it is (using the timeFormatter instance timeFormatter inside the method), then it also works with ARC.
I would recommend prefix instance variables for underlined properties, as in my code example, because Xcode does the same for automatically synthesized properties.
(I hope this helps and does not increase confusion!)