Why do I need to write @synthesize when I provide getter and setter?
Asked Answered
S

3

9

So the auto synthesize of properties is awesome. However, when you provide both a getter and a setter, you get an error.

@property (strong, nonatomic) NSArray *testArray;

- (NSArray *)testArray {
    return _testArray;
}

- (void)setTestArray:(NSArray *)testArray {
    _testArray = testArray;
}

Error: Use of undeclared identifier '_testArray'.

Adding @synthesize testArray = _testArray; solves the problem. I am just wondering why this is?

Seeder answered 16/10, 2012 at 15:38 Comment(4)
Seems you don't declare the ivar itself. Btw this is completely unrelated to Xcode. This wouldn't make a difference if you used Eclipse or nano and make to write your programs.Sherlynsherm
The @property is in there (and the question's about auto synthesized properties). Also, it works fine when you only implement only either the getter or the setter, it only fails when you do both.Seeder
the @property is declaring the iVarSocio
You should update the question: Adding @ synthesize testArray = _testArray; after @implementation className in dot m file solves the problem.Cause
P
8

When you provide both getter and setter, there is often just no need for an instance variable at all, i.e. when you just forward those messages or store data in other places.

As soon as one of them is missing, the ivar is needed to synthesize that functionality.

If I remember correctly, for readonly properties the analogue assumption holds as well.

Pot answered 16/10, 2012 at 15:44 Comment(2)
In my case I do need the backing store, since I need to do some initialization in the getter, and UI updating in the setter. So the @synthesize line is the way to go I guess? Too bad, I liked how clean the file is without them :)Seeder
Yes, in this case they are need - or you can put an ivar in your declaration list, even in your .m file.Pot
G
8

Under the most recent compiler/runtime, when you use

@synthesize testArray = _testArray;

the compiler creates

  • a _testArray ivar, if it doesn't already exists;
  • a testArray method, if you haven't implemented it;
  • a setTestArray: method, if you haven't implemented it.

If you use

@synthesize testArray;

the compiler creates a testArray ivar if it doesn't already exists, instead of _testArray.

If you don't use @synthesize, and rely on the autosynthesis, the _testArray ivar is created only if (both have to apply)

  • _testArray doesn't already exist;
  • at least one method has been synthesized (getter for readonly, getter and/or setter for readwrite).

In your example, you have implemented all methods, so the autosynthesis doesn't synthesize any, so it doesn't create the ivar. You can either declare the _testArray ivar yourself, or use an explicit synthesis as you did.

Gauze answered 16/10, 2012 at 15:56 Comment(3)
In your second part of your answer, I'm not sure you're required to have a getter/setter before it will generate the iVar. I have plenty of @property that don't have any explicit getter/setter with no explicit @synthesize and I can access them just fine via [self myProperty] or _myProperty. The modern runtime eliminates this boilerplate code.Socio
This is what I'm saying. If you have a @property, with no explicit @synthesize, and with no implementation for the getter/setter, then the getter/setter will be autosynthesized, therefore a _propertyname ivar will be created.Gauze
I have a question, in your example wasn't testArray already declared by specifying (at)property *testArray? Is one of the things that puzzles me, in my case I certainly don't need (at)synthesize because I created my own setter and getter method and already declared a (at)property yet if I don't have (at)synthesize I get an error. This is by far the best answer I have read, (at)Synthesize and (at)Property is very abstract.Gondola
R
-1

I did some testing:

Under recent Objective-c convention, you do not need to synthesize properties.

If you do

@property (strong, nonatomic) Business* theBiz;

iOs will automatically create a private ivar variable called _theBiz;

If you only implement a getter or a setter it seems to work just fine:

-(void)setTheBiz:(Business *)theBiz
{
    _theBiz = theBiz;
}

However, if you declare BOTH, even if one of them are just empty function you'll get compile error.

-(void)setTheBiz:(Business *)theBiz
{
    _theBiz = theBiz;
}

-(Business *) theBiz
{

}

When you implement BOTH getter and setter you will get a compile error saying that therre is no such thing as _theBiz.

That can be easily fixed by adding:

@synthesize theBiz = _theBiz;

But that defeat the whole point of this awesome new feature of Objective-c.

I wonder if this is by design or I am just missing something. What does apple thing?

My best guess is it's a bug.

Guillaume's answer doesn't address that fact.

He said that

at least one method has been synthesized

It seems that _theBiz would have been created if only ONE of getter or setter is set. When both are set it's no longer created and that must be a bug. In fact, none have to be set at all and the ivar will still be created.

The only way to fix that is to explicitly do

@synthesize theBiz = _theBiz;
Reginaldreginauld answered 19/10, 2012 at 8:59 Comment(3)
Guillaume's answer does address this fact: "In your example, you have implemented all methods, so the autosynthesis doesn't synthesize any, so it doesn't create the ivar. You can either declare the _testArray ivar yourself, or use an explicit synthesis as you did." Also, your answer is basically exactly what my question was :)Seeder
Guillaume said this: "at least one method has been synthesized (getter for readonly, getter and/or setter for readwrite)." That is false. If BOTH method got synthesized ivar is NOT created. Also there is no reason not to create ivar if you have both methods implemented.Reginaldreginauld
I should just delete this answer if you don't like it. I still think it's a bug.Reginaldreginauld

© 2022 - 2024 — McMap. All rights reserved.