Reputation: 2543
I'm trying to add a property without creating an instance variable. Is it possible to do this? Or can you do something similar in a way that's not a property?
Example:
@interface RandomClass()
@property (nonatomic) int value;
@end
@implementation RandomClass
@synthesize value = _value;
// Here I override the default methods @synthesize
-(int)value
{
return 8; // Actually I'm returning something more complex, so a "define" won't work
}
-(void)setValue:(int)value
{
self.someOtherValue = value;
}
In the code above, I'm not using the instance variable _value
! Is there a way to do this without creating the variable?
Upvotes: 12
Views: 3023
Reputation: 185671
Remove the line
@synthesize value = _value;
Since you're implementing the getter/setter yourself, the @synthesize
isn't helpful.
@synthesize
serves two jobs. The first job is to connect the property to a backing ivar, synthesizing the ivar if it doesn't already exist. The second job is to synthesize the getter/setter. If you don't need the backing ivar, and if you're providing implementations for the getter/setter yourself, then you don't need the @synthesize
at all.
Upvotes: 8