I switched to Xcode 4 recently and I don’t really understand this new way to write accessors.
For example, in the application delegate class that is auto-generated when creating a new project, the window object is not declared in the @interface but just this way:
@property (nonatomic, retain) IBOutlet UIWindow *window;
Then, in the implementation file, we have the @synthesize window=_window;.
And in the functions, we have either self.window OR _window.
For example:
[self.window makeKeyAndVisible]; // in didFinishLaunchingWithOptions function
[_window release]; // in dealloc function
Can you explain me the difference, why there is nothing in the @interface, why we do @synthesize window=_window; instead of @synthesize window; and what is the difference between self.window and _window, I mean when do I have to call one more than the other?
I’m a bit lost, and feel like the new code I doing trying to do the same in not working properly…
Thanks!
“Why is there nothing in the
@interface“The runtime is synthesizing the ivar for you.
“Why do we do
@synthesize window=_window;This means that the
windowproperty will use an ivar named_window(by default the ivar name is the name of the property)“What is the difference between
self.windowand_window?”The former is using the
window“getter” method (ie,foo = [self window]), and the latter is accessing the ivar directly.“Why do I have to call one more than the other?”
It is generally considered unsafe to use accessor methods in your
deallocmethod, which means using the ivar is preferred.