Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Mobile Development > BOOK: Beginning iOS 5 Application Development
Password Reminder
Register
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old June 7th, 2012, 10:09 AM
Registered User
Points: 23, Level: 1
Points: 23, Level: 1 Points: 23, Level: 1 Points: 23, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2012
Posts: 5
Thanks: 2
Thanked 0 Times in 0 Posts
Question Empty Application Chapter4 Page 81

Code:
@synthesize window = _window;
@synthesize viewController = _viewController;
I understand that we are initializing accessor methods for window and viewController but whats the _windows & _viewController?
Reply With Quote
  #2 (permalink)  
Old June 9th, 2012, 02:18 PM
Friend of Wrox
Points: 1,671, Level: 16
Points: 1,671, Level: 16 Points: 1,671, Level: 16 Points: 1,671, Level: 16
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2010
Posts: 298
Thanks: 1
Thanked 62 Times in 60 Posts
Default

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
Code:
#import <UIKit/UIKit.h>

@class HelloWorldViewController;

@interface EmptyAppAppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;

@property (strong, nonatomic) HelloWorldViewController *viewController;    

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

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

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

Code:
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:

Code:
-(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.
Code:
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
Code:
_variable = newVariable;
Bob
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 12:28 AM.


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