I've been reading that with the lastest version of Xcode you don't even need to use synthesize to generate ivar, getters and setters, that Xcode itself handles this for you and creates something like _youIvarName for you, however after trying this out i wasn't able to make this work.

Even if i declare my properties, when i try to access these _yourVarName Xcode says such a variable doesn't exist.

example

Header file:

@interface ComplexNumber : NSObject
@property  double realPart;
@property  double imaginaryPart;

Implementation:

#import "ComplexNumber.h"

@implementation ComplexNumber


-(double)modulus
{
    return sqrt(_realPart*_realPart + _imaginaryPart*_imaginaryPart);
}

-(void)setRadius:(double)aRadius phase:(double)aPhase
{
    [self setRealPart] = aRadius * cos(aPhase);
    self.imaginaryPart = aRadius * sin(aPhase);
}

-(void)print
{
    NSLog(@"%g + %gi", realPart, imaginaryPart);
}

None of these tries to access those _yourIvar worked... any clues on what I'm not grasping here ?

EDIT:

My main question is, if I declare a @property is Xcode going to automatically generate getters, setters and instance variables (with a leading underscore, _myVar) without the need to use @synthesize? That's something I read here on stackoverflow and I'm not sure it really works.

有帮助吗?

解决方案

Almost everything looks like it should work. The one line that isn't valid objective-C is

[self setRealPart] = aRadius * cos(aPhase);

which should instead be

[self setRealPart:aRadius * cos(aPhase)];

could that be the error you're seeing?

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top