Justin Copeland
Justin Copeland

Reputation: 1951

How come synthesizing with an underscore is not working?

I am trying to synthesize variables in my iPhone app with

@synthesize samples=_samples;

with samples declared as

@property (strong, nonatomic) NSMutableArray *samples;

However, I get a build error claiming that _samples does not exist. Why?

Upvotes: 1

Views: 163

Answers (2)

JoeCortopassi
JoeCortopassi

Reputation: 5093

Are you trying to access the _samples from outside the implementation file? ivars generated through @synthesize are not viewable by anything outside of the implementation where the @synthesize was called. So if you do something like this...

MyView *myView = [[MyView alloc] init];
myView._sample;

...you will see an error. See here for more details: https://stackoverflow.com/a/8511046/251012 .

.

.

.

EDIT: All of the below is wrong. Left, so that the comments make sense

Are you declaring your ivar's and are the names spelled correctly?

When you say something like...

@synthesize foo = _foobar;

...you need to make sure that you set the instance variable in your interface like so...

@interface MyObject : NSObject
{
    NSString *_foobar;
}
@property(nonatomic,strong) NSString *foo;
@end

To be clear, when you say foo=_foobar, foo is the base name to auto-generate the getter/setter's, and _foobar is the name of the ivar. If no ivar is declared, @property will auto-generate one of the same name.

Upvotes: 2

MrWaqasAhmed
MrWaqasAhmed

Reputation: 1489

Same code is working on my side. Try to restart xcode and rebuild the project.

Upvotes: 1

Related Questions