Writable atomic property cannot pair synthesized setter/getter

1.7k Views Asked by At

I have the following Dog.h and Dog.m and getting the error in the title. How can I change the code to get this to work?? Would really appreciate some insight!

   #import <Foundation/Foundation.h>

@interface Dog : NSObject

@property int age;


-(void) setAge: (int) value;
-(int) returnAge;

@end



 #import "Dog.h"

@implementation Dog

@synthesize age;


-(void) setAge: (int) value
{
    age = value;
}

-(int) returnAge
{
    return age;
}

@end
1

There are 1 best solutions below

0
Rob Sanders On
 #import <Foundation/Foundation.h>

@interface Dog : NSObject

@property (nonatomic) int age;

@end



 #import "Dog.h"

@implementation Dog

@synthesize age = _age;


- (void)setAge:(int)age
{
    _age = age;
}

- (int)age
{
    return _age;
}

@end

The _age variable should only be used in the getter and setter methods. If you access the variable elsewhere in the .m file you should use age or self.age. And from outside the .m file you should use dog.age.