When to access properties using "self"

I read a number of questions on this site about this problem, I understand the following:

self.property refers to the getter / setter method, either manually or through @synthesize. Depending on whether the property has been declared as saving, copying, etc., the Save account changes correctly, for example, a saved property, releases the previous value assigned to the new value with "save", and increases the number of deductions by 1.

Properties are usually declared with instance variables of the same name (they can be different if you perform the assignment manually). This is usually due to the fact that accessors created using @synthesize use an instance variable to reference an object in memory, and then execute the appropriate commands.

My question is based on the fact that in many examples self.property and the property are used interchangeably for different things, and I am having problems defining rules. One example in the sample Recipes example in Apple Docs:

self.navigationItem.title = recipe.name; nameTextField.text = recipe.name; overviewTextField.text = recipe.overview; prepTimeTextField.text = recipe.prepTime; 

and...

 self.ingredients = sortedIngredients; 

Each of these properties has associated private instance variables of the same name. All are declared identically with non-atomic, persistent attributes. Each one is released at dealloc ...

Still, the "ingredients" are available through self, and the "prepTimeTextField" is available directly.

What is the reason for differences in access methods?

But what if I get access to the view delegate? Or a core-data object that was passed to the view controller by its previous view controller as a stored property?

Many thanks

+6
properties objective-c self
source share
3 answers

you almost always want to access variables using synthesized setters / getters, even if at the moment you are not doing anything special.

if, as you develop your application, you find that you need to perform additional checking / formatting of variables, then you can simply implement the required setter / getter method, and if you use synthesized methods, this code will be called.

This is usually a good habit.

+2
source share

If you declare your property as follows:

 @property (nonatomic, retain) NSMutableArray *myArray; 

The generated setter will automatically save the value to be transmitted. This is why you sometimes see this in the Apple sample example:

 self.myArray= [[NSMutableArray alloc] init]: [self.myArray release]; 

"[self.myArray release]" is required because the hold count is two after the first line. Exemption from it ensures that it is up to 1 (where it should be).

If I just use automatically created setters, I will not use them when working from within the class. This is much easier for me:

 myArray = [[NSMutableArray alloc] init]; 

against. example above.

My two cents.

+1
source share

It depends. If your instance variables are unthinkable, you can directly access them. But the reason most of us like to access members through properties is because we can change something about the logic of this member, and everything will work.

For example, sometimes you need to change a class to load something lazily, and not right away. If you are already using properties to access your instance variable, you can simply edit the access method and everything should work.

0
source share

All Articles