Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS Xcode 4 properties access

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!

like image 480
Dachmt Avatar asked May 25 '11 20:05

Dachmt


People also ask

What is a property list iOS?

The Information Property List ( Info.plist ) is a required iOS file that provides information about your application's configuration to the system.

How do I open an accessibility inspector?

Accessing the Accessibility InspectorChoose Accessibility in the Tools > Browser Tools menu. Select the Accessibility tab in the Developer Tools toolbox. Right-click in the main browser window, and choose Inspect Accessibility Properties in the context menu.


1 Answers

  1. "Why is there nothing in the @interface"

    The runtime is synthesizing the ivar for you.

  2. "Why do we do @synthesize window=_window;

    This means that the window property will use an ivar named _window (by default the ivar name is the name of the property)

  3. "What is the difference between self.window and _window?"

    The former is using the window "getter" method (ie, foo = [self window]), and the latter is accessing the ivar directly.

  4. "Why do I have to call one more than the other?"

    It is generally considered unsafe to use accessor methods in your dealloc method, which means using the ivar is preferred.

like image 101
Dave DeLong Avatar answered Sep 24 '22 21:09

Dave DeLong