Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Objective C synthesized variable underscore prefix not working

Tags:

objective-c

I was reading the tutorials in the official Apple site about synthesizing properties at Apple. Here is an excerpt:

Unless you specify otherwise, the synthesized instance variable has the same name as the property, but with an underscore prefix. For a property called firstName, for example, the synthesized instance variable will be called _firstName.

However, later, it says:

Important: If you use @synthesize without specifying an instance variable name, like this: @synthesize firstName; the instance variable will bear the same name as the property. In this example, the instance variable will also be called firstName, without an underscore.

These statements appear to be in disagreement. When I am using synthesize like synthesize numerator, and later trying to use _numerator, it is showing the following error: use of undeclared identifier _numerator.

enter image description here

Any idea what I am doing wrong?

like image 667
SexyBeast Avatar asked Jan 10 '23 04:01

SexyBeast


2 Answers

You can declare instance variables and properties in the @interface.

In the implementation, you can use

@synthesize property = instancevariable;

When you do that, the compiler creates an instance variable named "instancevariable" if it doesn't exist yet, and generates code for the setter and getter as needed. The variable name is anything that you want to use.

@synthesize property;

on its own is the same as

@synthesize property = property; 

which means an instance variable is created with the same name as the property, if it doesn't yet exist. Whether you created an instance variable yourself starting with an underscore doesn't matter. That instance variable will be just an instance variable, possibly causing major confusion in your code.

// No synthesize statement

is exactly the same as

@synthesize property = _property;

which means an instance variable is created with a leading underscore, if it doesn't yet exist. Whether you created an instance variable yourself without an underscore doesn't matter. That instance variable will be just an instance variable, possibly causing major confusion in your code. In this case, the compiler will give a warning.

There is one exception: If you implemented all the required methods (both setter and getter, or just the getter for a readonly proper) yourself, and you don't use @synthesize, then no instance variable will be created. If you use @synthesize, an instance variable will be created as described above.

So the best choice is to just declare the @property and nothing else; an instance variable starting with an underscore will be created. If you implement both setter and getter, or just the getter of a readonly property, you may not need an instance variable. If you need one, you can declare the instance variable or create it using @synthesize.

like image 161
gnasher729 Avatar answered Jan 29 '23 19:01

gnasher729


If I don't use synthesize, and don't declare setters and getters for variables as well, how will they be accessible?

Recent versions of Objective-C make @synthesize the default for properties, so you don't need to explicitly write @synthesize firstName; -- the compiler will take care of that for you.

As @TimReddy points out in a comment, the difference between the two passages that you quoted is that one is talking about behavior when you use the @synthesize directive explicitly (the ivar gets the same name as the property), while the other describes behavior when the compiler synthesizes the ivar automatically (the ivar name is the property name with an underscore prefix).

like image 45
Caleb Avatar answered Jan 29 '23 20:01

Caleb