Amazon.com Widgets Basic question about synthesized accessor methods
Welcome, Guest. Please login or register.
Did you miss your activation email?
May 25, 2013, 08:43:33 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
| | |-+  Basic question about synthesized accessor methods
Pages: [1]   Go Down
Print
Author Topic: Basic question about synthesized accessor methods  (Read 453 times)
jonr
Jr. Member
**
Posts: 52






« on: April 15, 2012, 07:29:59 PM »

I am understanding this but not well enough.  When this material is introduced at the bottom of pg. 132 it doesn't really explain fully what how your properties are supposed to be named, or what they are.  On the next page you get info that clarifies the process of the generation of the setter and getter methods, so the concept behind it gets clarified.

But, back to pg. 132, the beginning of this:  It states that 'These properties are often named the same as your instance variables, although they don't have to be.'  In our example, it so happens that the instance variables are the same names as what is needed to get the correct naming for the setter and getter methods.

But what about an example where the names of the instance variables *are not* used as the names of the properties?  Isn't the main criteria/goal of naming the properties to end up with the synthesized setter and getter methods of the proper names?  It just seems that the fact that the properties *could* be named the same as the instance variables (as in this example) is just a specific condition for the types of methods that need to be created in this example.  Also, the '@property' statement also uses 'int' before the instance variables to presumably identify them as ints.  So, again, what if the properties are not the same as the instance variables?  Is the data type then not needed for this statement?
Logged
rue
Jr. Member
**
Posts: 53






« Reply #1 on: April 24, 2012, 09:10:29 AM »

jonr, your question intrigued me so I played with the samples.

I found out I could even totally remove the variable declarations in @implementation section if I'm using @property and @synthesize and the program will still work.

Code: (Objective-C)
@implementation Fraction
/*{
    int numerator;
    int denominator;
}
*/

@synthesize numerator;
@synthesize denominator;

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

-(double) convertToNum {
    if (denominator !=0)
        return (double)numerator/denominator;
    else {
        return NAN;
    }
}

-(void) setTo: (int)n over:(int)d {
    numerator = n;
    denominator = d;
}

-(void) add: (Fraction *) f {
    numerator = (numerator * f.denominator) + (denominator * f.numerator);
    denominator = denominator * f.denominator;
}
@end
« Last Edit: April 24, 2012, 09:14:46 AM by rue » 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.