Wrox Programmer Forums
BOOK: Beginning iOS 5 Application Development
This is the forum to discuss the Wrox book Beginning iOS 5 Application Development by Wei-Meng Lee; ISBN: 978-1-1181-4425-1
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning iOS 5 Application Development section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old June 7th, 2012, 10:09 AM
Registered User
Join Date: Jun 2012
Posts: 5
Thanks: 2
Thanked 0 Times in 0 Posts
Question Empty Application Chapter4 Page 81

@synthesize window = _window;
@synthesize viewController = _viewController;
I understand that we are initializing accessor methods for window and viewController but whats the _windows & _viewController?
Old June 9th, 2012, 02:18 PM
Friend of Wrox
Join Date: Aug 2010
Posts: 298
Thanks: 1
Thanked 62 Times in 60 Posts

Proper memory management is best handled by using accessors.The pattern used in the project is useful in helping the programmer avoid unintentional direct setting of instance variables. It also also prevents data hiding when local variables have the same name as instance variables.

From the example in the book the header file
#import <UIKit/UIKit.h>

@class HelloWorldViewController;

@interface EmptyAppAppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;

@property (strong, nonatomic) HelloWorldViewController *viewController;    

could have had in the .m file the following
@synthesize window;

@synthesize viewController;
This would have allowed both of the following

viewController = [[[HelloWorldViewController alloc] initWithNibName:@"HelloWorldViewController" bundle:nil] autorelease];

self.viewController = [[[HelloWorldViewController alloc] initWithNibName:@"HelloWorldViewController" bundle:nil] autorelease];
These are not the same.
The first one sets the variable directly and the second uses the accessor. The first version has an object ownership problem, since the object will be deallocated when autorelease is called and viewController will probably end up pointing to garbage. The second one would have a synthesized setter similar to the following:

-(void)setViewController:(HelloWorldViewController *)aViewController
    [aViewController retain];
    [viewController release];
    viewController = aViewController;
Where the variable in is retained to balance the autorelease. The old viewController is released so that it is deallocated unless retained by another object somewhere else, and the viewController stores the pointer to variable in.
The @synthesize variable = _variable; pattern will result in a compiler error if an attempt to directly set the variable e.g.
variable = newVariable;
is attempted.
On the rare occasion where the variable does need to be acted on directly you call the variable with the underscore
_variable = newVariable;

Similar Threads
Thread Thread Starter Forum Replies Last Post
Chapter 4, Page 81, Seeding a Database Drogil BOOK: Professional ASP.NET MVC 3 3 August 3rd, 2013 01:45 AM
Error following directions on page 81. rnes BOOK: Windows Phone 7 Application Development: 24 Hour Trainer 1 January 30th, 2012 06:14 PM
Error Message on Page 81 vbboyd BOOK: Professional ASP.NET MVC 3 0 October 25th, 2011 01:54 AM
Confused between onSaveInstanceState and onPause on page 81 and 82 fungi8210 BOOK: Professional Android 2 Application Development 1 March 9th, 2010 09:26 AM
Manifest Editor - Application tab shows empty page sunrain BOOK: Professional Android Application Development ISBN: 978-0-470-34471-2 6 January 21st, 2009 04:42 PM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.