Welcome, Guest. Please login or register.
Did you miss your activation email?
April 30, 2017, 11:46:59 AM
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
| | |-+ Are variables not needed if using @property/@synthesize?
Pages: [1] Go Down
Author Topic: Are variables not needed if using @property/@synthesize? (Read 2118 times)
Jr. Member
Posts: 53

on: April 24, 2012, 09:16:09 AM

I just found out a few minutes ago, I could totally remove the variable declarations in @implementation section if I'm using @property and @synthesize and the program will still work.

Is this correct? bad practice? What are the pros and cons of this?

Code: (Objective-C)
@implementation Fraction
    int numerator;
    int denominator;

@synthesize numerator;
@synthesize denominator;

-(void) print {

-(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;
Full Member
Posts: 177

Reply #1 on: April 24, 2012, 01:00:44 PM

When using the @property / @synthesize directive, the variables will be "automatically" declared, so you won't need to do it yourself. You CAN do it, if you feel like it makes the class more readable, but it is not a requirement and there is really no recommended way, it's up to you.

Best Regards -
If you think i helped you could return the favor Wink

Subscribe to my channel on youtube: http://www.youtube.com/user/JBJProgramming

My apps:
- iTap Fruits (all countries)
- Toilet-Quiz (Denmark only)
- Zoo Kids (Denmark only)
- Diablo Item Database (All countries)

Posts: 5

Reply #2 on: January 19, 2013, 05:12:47 PM

As I understand it, one reason for creating your setter manually is if, for example, you wish to use multiple arguments.
Pages: [1] Go Up
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.