Forums
New posts
Articles
Product Reviews
Policies
FAQ
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Menu
Log in
Register
Install the app
Install
Forums
macOS & iOS Developer Playground
iOS Development
Very fundamental question in Obj-C
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
<blockquote data-quote="Mark FX" data-source="post: 1407947" data-attributes="member: 211556"><p>The old fashioned way to do synthesized properties, was to declare an instance variable with the same name as the property like this.</p><p></p><p>[CODE]</p><p>//myClass.h</p><p></p><p>#import <UIKit/UIKit.h></p><p></p><p>@interface myClass : NSObject</p><p>{</p><p> double topSpeed;</p><p>}</p><p></p><p>@property (nonatomic, readwrite, assign) double topSpeed;</p><p></p><p>@end</p><p></p><p>//myClass.m</p><p></p><p>#import "myClass.h"</p><p></p><p>@implementation myClass</p><p></p><p>@synthesize topSpeed;</p><p></p><p>@end</p><p>[/CODE]</p><p></p><p>By default, a synthesized property will access the instance variable of the same name, so the topSpeed property will access the topSpeed instance variable, and the setter and getter methods will also set or return the instance variable's value.</p><p></p><p>But the latest trend with using synthesized properties, is to let the @sythensize command automatically create an instance variable of the same name for you, which is what it does if you have not created one yourself.</p><p></p><p>So the above code can be written like this.</p><p></p><p>[CODE]</p><p>//myClass.h</p><p></p><p>#import <UIKit/UIKit.h></p><p></p><p>@interface myClass : NSObject</p><p></p><p>@property (nonatomic, readwrite, assign) double topSpeed;</p><p></p><p>@end</p><p></p><p>//myClass.m</p><p></p><p>#import "myClass.h"</p><p></p><p>@implementation myClass</p><p></p><p>@synthesize topSpeed;</p><p></p><p>@end</p><p>[/CODE]</p><p></p><p>Which will automatically create an instance variable for you called topSpeed.</p><p></p><p>Then suppose, you want to change the name of the automatically created instance variable </p><p>to a different name, then you would do this.</p><p></p><p>[CODE]</p><p>//myClass.h</p><p></p><p>#import <UIKit/UIKit.h></p><p></p><p>@interface myClass : NSObject</p><p></p><p>@property (nonatomic, readwrite, assign) double topSpeed;</p><p></p><p>@end</p><p></p><p>//myClass.m</p><p></p><p>#import "myClass.h"</p><p></p><p>@implementation myClass</p><p></p><p>@synthesize topSpeed = carTopSpeed;</p><p></p><p>@end[/CODE]</p><p></p><p>This means that you would get an set the topSpeed property and created instance variable</p><p>with this code.</p><p></p><p>[CODE]</p><p> carsTopSpeed = 120.00;</p><p></p><p> NSLog(@"topSpeed = %f", carsTopSpeed)"</p><p>[/CODE]</p><p></p><p>So in trying to awnser your question, all of the above code snippets are valid, but the last is</p><p>a way of you using a different instance variable name for your synthesized properties.</p><p></p><p>Apple's prefered instance variable prefix is to use an undersore "_", and also the Xcode project templates are set to also use the underscore "_" prefix, for any properties it creates for it's projects.</p><p></p><p>A good reason for using the underscore prefix, is when your creating user interface properties, like windows buttons or text fields, so that when your writing your code, you don't get naming conflicts with commonly used words, and class message commands, like this.</p><p></p><p>[CODE]</p><p>@synthesize window = _window;</p><p></p><p>- (void)displayWindow</p><p>{</p><p> [_window makeKeyAndVisible];</p><p>}</p><p>[/CODE]</p><p></p><p>Hope this is of some help.</p><p></p><p>Regards Mark</p></blockquote><p></p>
[QUOTE="Mark FX, post: 1407947, member: 211556"] The old fashioned way to do synthesized properties, was to declare an instance variable with the same name as the property like this. [CODE] //myClass.h #import <UIKit/UIKit.h> @interface myClass : NSObject { double topSpeed; } @property (nonatomic, readwrite, assign) double topSpeed; @end //myClass.m #import "myClass.h" @implementation myClass @synthesize topSpeed; @end [/CODE] By default, a synthesized property will access the instance variable of the same name, so the topSpeed property will access the topSpeed instance variable, and the setter and getter methods will also set or return the instance variable's value. But the latest trend with using synthesized properties, is to let the @sythensize command automatically create an instance variable of the same name for you, which is what it does if you have not created one yourself. So the above code can be written like this. [CODE] //myClass.h #import <UIKit/UIKit.h> @interface myClass : NSObject @property (nonatomic, readwrite, assign) double topSpeed; @end //myClass.m #import "myClass.h" @implementation myClass @synthesize topSpeed; @end [/CODE] Which will automatically create an instance variable for you called topSpeed. Then suppose, you want to change the name of the automatically created instance variable to a different name, then you would do this. [CODE] //myClass.h #import <UIKit/UIKit.h> @interface myClass : NSObject @property (nonatomic, readwrite, assign) double topSpeed; @end //myClass.m #import "myClass.h" @implementation myClass @synthesize topSpeed = carTopSpeed; @end[/CODE] This means that you would get an set the topSpeed property and created instance variable with this code. [CODE] carsTopSpeed = 120.00; NSLog(@"topSpeed = %f", carsTopSpeed)" [/CODE] So in trying to awnser your question, all of the above code snippets are valid, but the last is a way of you using a different instance variable name for your synthesized properties. Apple's prefered instance variable prefix is to use an undersore "_", and also the Xcode project templates are set to also use the underscore "_" prefix, for any properties it creates for it's projects. A good reason for using the underscore prefix, is when your creating user interface properties, like windows buttons or text fields, so that when your writing your code, you don't get naming conflicts with commonly used words, and class message commands, like this. [CODE] @synthesize window = _window; - (void)displayWindow { [_window makeKeyAndVisible]; } [/CODE] Hope this is of some help. Regards Mark [/QUOTE]
Verification
Name this item. 🍎
Post reply
Forums
macOS & iOS Developer Playground
iOS Development
Very fundamental question in Obj-C
Top