0

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.

jscs
  • 63,694
  • 13
  • 151
  • 195
thiagocfb
  • 487
  • 1
  • 9
  • 19
  • Is the compiler build setting for your project set to LLVM 4.1? – rmaddy Oct 23 '12 at 01:44
  • hmm I haven't changed anything related to that (I don't even know when can i change such a setting) – thiagocfb Oct 23 '12 at 01:49
  • 1
    Your understanding is correct as of Xcode 4.5 but I'm not sure why you can't access the ivar with an underscore. Have you accepted all the recommended updates to the project (usually offered when you click on the project)? – Robotic Cat Oct 23 '12 at 01:57
  • Also, try clicking "validate settings" on the project settings to make sure you're using the latest compiler. – cobbal Oct 23 '12 at 02:00
  • I'm using Xcode 4.5, I believe I've updated everything that was needed... I really don't get what's wrong if that affirmation is really correct. Could you please give me an example of how to work with this correctly ? I mean, this feature of `@property` auto generating ivars without `@synthesize` – thiagocfb Oct 23 '12 at 02:01

1 Answers1

1

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?

cobbal
  • 69,903
  • 20
  • 143
  • 156
  • I've restarted Xcode and everything is working now ! You were right :) Thanks a lot :DD And yes, that line was a typo of mine, I've already fixed it ! I was having a really hard time understanding how to work with ivars, `@property`, getters and setters @_@ – thiagocfb Oct 23 '12 at 02:10