iOS very fundamental questions

zijianz

macrumors newbie
Original poster
May 21, 2012
7
0
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.
 

dejo

Moderator
Staff member
Sep 2, 2004
15,981
450
The Centennial State
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.
No, they're not automatic. That's why you need to include the @synthesize directive in your implementation file. Otherwise, you get a warning.

I wonder why do we use _topSpeed? Since we didn't declare a variable called _topSpeed.
Newer versions of Xcode will automatically create an instance variable called _topSpeed, with the same datatype as the property, for you, based on your synthesizer.

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?
topSpeed is a property name. _topSpeed is an instance variable name. The former doubles as a method name since the @synthesize will create a getter of:
Code:
- (double)topSpeed;
and a setter of:
Code:
- (void)setTopSpeed:(double)topSpeed;
The instance variable that backs the property only gets generated if you include the assignment in the @synthesize directive:
Code:
@synthesize something = _something
 

Similar threads

  • PinkyMacGodess
2
Replies
2
Views
394
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.