I’ve got a simple question about objective-c.
I’ve been taught to lazily instantiate my properties by synthesizing them and then overriding the getter in the following manner.
-(SomeClass *)someProperty {
if ( !_someProperty )
_someProperty = [[SomeClass alloc] init ]
return _someProperty
}
What happens when I don’t do this? I ask this because I have properties that are not allocated & instantiated in this manner and they seem (maybe not) to behave just fine.
My only guess is that if a property is unallocated & instantiated it will be left as nil; in which case messaging nil would do nothing. Is this correct?
Thanks in advance for your help!
Yes, messaging nil is just fine. You don’t have to override any getters to lazily initialize your properties. See this: https://developer.apple.com/library/ios/documentation/cocoa/conceptual/objectivec/Chapters/ocObjectsClasses.html#//apple_ref/doc/uid/TP30001163-CH11-SW7