Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
March 21, 2018, 10:53:05 PM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
   Home   Help Search Login Register Chat  
Pages: [1]   Go Down
Author Topic: Are variables not needed if using @property/@synthesize?  (Read 2262 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:

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:  

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 ゥ 2009 All rights reserved.