Amazon.com Widgets Synthesized Accessor Methods naming convention in Implementation file
Welcome, Guest. Please login or register.
Did you miss your activation email?
May 18, 2013, 10:21:18 PM
Home Help Search chat Login Register   
News: Read this please.The Great Kangaroo Escape Looking for reviews of the 4th ed on Amazon!   Twitter:  @skochan
                     

+  Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
|-+  Programming in Objective-C, 4th edition
| |-+  Chapter 7
| | |-+  Synthesized Accessor Methods naming convention in Implementation file
Pages: [1]   Go Down
Print
Author Topic: Synthesized Accessor Methods naming convention in Implementation file  (Read 231 times)
Rfields
Newbie
*
Posts: 5






« on: January 04, 2013, 12:50:06 PM »

Since I'm using xcode 4.5 I decided to try leaving out the

@synthesize numerator, denominator; as the text says it is now no longer necessary (I have the @property int numerator,denominator; in the @interface file).

In the implementation file for the FractionTest example, if I do the following I get a ' Use of Undeclared identifier 'numerator'...'denominator' error:

@implementation Fraction

-(void) print
{
    NSLog(@"%i/%i", numerator,denominator);
}

@end


I have to either add the @synthesizer or change it to _numerator,_denominator.
Why do I need the underscore? the text says: 
In general, if you have a property called x, including the following line in your implementation section causes the compiler to automatically synthesize a getter method called x and a setter method called setX:

thank you

Logged
afterDark
Global Moderator
Full Member
*****
Posts: 126






« Reply #1 on: January 04, 2013, 03:05:13 PM »

The complete quote from the book is actually this:

Quote
In general, if you have a property called x, including the following line in your implementation section causes the compiler to automatically synthesize a getter method called x and a setter method called setX:.

@synthesize x;

So you need to explicitly use @synthesize if you want the instance variable name to be x. If you leave out the @synthesize directive, the 'standard' setter, getter and variable names will be used. Which means: variable names starting with an underscore _, as if you would have written:

@synthesize x = _x;

More explanation is in the Mac Developer Documentation ( https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ProgrammingWithObjectiveC/EncapsulatingData/EncapsulatingData.html , see under 'Most Properties are Backed by Instance Variables)

Quote
Unless you specify otherwise, the synthesized instance variable has the same name as the property, but with an underscore prefix. For a property called firstName, for example, the synthesized instance variable will be called _firstName.

Directly below that in the same document it says (under You can Customize Synthesized Variable Names)

Quote
As mentioned earlier, the default behavior for a writeable property is to use an instance variable called _propertyName.

If you wish to use a different name for the instance variable, you need to direct the compiler to synthesize the variable using the following syntax in your implementation:

@implementation YourClass

@synthesize propertyName = instanceVariableName;
...
@end

Admittingly, the case of what happens when you use @synthesize propertyName without the = is not discussed explicitly there.

In your code, you could for instance use

NSLog(@"%i,%i", self.numerator, self.denominator)

Sorry, a quite lengthy response... hope it helps!
Logged

I am just an amateur with Objective-C, don't let the moderator label fool you. Working my way through the book slowly.
Rfields
Newbie
*
Posts: 5






« Reply #2 on: January 04, 2013, 06:39:00 PM »

Thank you afterDark for taking the time to explain.
Logged
Pages: [1]   Go Up
Print
Jump to:  



Login with username, password and session length

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Entire forum contents (c) 2009 classroomM.com. All rights reserved.