Very fundamental question in Obj-C

Joined
May 21, 2012
Messages
1
Reaction score
0
Points
1
First, I am really new in this area, please forgive my slow,
I saw codes like
'@property (nonatomic) double topSpeed; ' in the header file to declare a public variable in class. It will create a setter and getter automatically.

And we declare the implementation to the setter getters in the implementation file like
"@synthesize topSpeed = _topSpeed"

I wonder why do we use _topSpeed? Since we didn't declare a variable called _topSpeed.

To be more straightforward, what are topSpeed and _topSpeed exactly? Are those both variables or methods name? Is that mean whenever we declared @property type something, it will automatically generate a variable called _something?

THank you.
 
Joined
May 7, 2012
Messages
2
Reaction score
0
Points
1
Location
Brazil
Your Mac's Specs
MBP I5 2.4Ghz 8Gb 13"
First, I am really new in this area, please forgive my slow,
I saw codes like
'@property (nonatomic) double topSpeed; ' in the header file to declare a public variable in class. It will create a setter and getter automatically.

And we declare the implementation to the setter getters in the implementation file like
"@synthesize topSpeed = _topSpeed"

I wonder why do we use _topSpeed? Since we didn't declare a variable called _topSpeed.

To be more straightforward, what are topSpeed and _topSpeed exactly? Are those both variables or methods name? Is that mean whenever we declared @property type something, it will automatically generate a variable called _something?

THank you.


Take these questions as mine too!!!!
 
Joined
Aug 13, 2011
Messages
200
Reaction score
7
Points
18
Location
West Sussex
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

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

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

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)"

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];
}

Hope this is of some help.

Regards Mark
 
Joined
May 7, 2012
Messages
2
Reaction score
0
Points
1
Location
Brazil
Your Mac's Specs
MBP I5 2.4Ghz 8Gb 13"
Thanks Mike!!! I got the idea. Everything that you have explained make all sense.
 

Shop Amazon


Shop for your Apple, Mac, iPhone and other computer products on Amazon.
We are a participant in the Amazon Services LLC Associates Program, an affiliate program designed to provide a means for us to earn fees by linking to Amazon and affiliated sites.
Top