Objective-C Naming Conventions - Clarification

I have doubts. This is not obvious to me.

Suppose we have an @property named myProperty in a class. With synthesis, we can do two things. Either we can assign an ivar , say _someIVar , or just just omit it, in which case the compiler will take care of creating an ivar with the same name, myProperty , for that property. In the first case, we can access the property using ivar _someIVar . In the second case, we can use the ivar myProperty , which was generated by the compiler for us.

Now I doubt, when we push another view controller from a view controller that is already on the navigation stack, we use

[self.navigationController pushViewController:newViewController animated:YES];

      

Since navigationController is a property, I assume it should have an ivar assigned to it. There are only two possibilities.

1) First, Apple has a convention for naming ivars with a leading underscore (_) . If so, I can call the pushViewController: animated: method like this,

[_navigationController pushViewController:newViewController animated:YES];

      

2) The second possibility is obviously ivar without underscore,

[navigationController pushViewController:newViewController animated:YES];

      

But the compiler won't let me access the navigationController anyway. What for? Is this something to do with private property or something, or am I just not understanding Objective-C at all?

+3


source to share


3 answers


You don't know what Apple did to implement the navigationController property, so you can't just assume there is a _navigationController ivar plugin somewhere in the UIViewController class. Example: a property can be declared @dynamic, in which case the implementation can be absolutely any.



There is no way to guess that ivar supports a property just by looking at the property itself.

+3


source


You cannot guess the name of the ivar. What's more, you shouldn't try to access ivars declared in superclasses. And third, there is no guarantee that the property is navigationController

fully supported by ivar, it can be backed up by a custom getter that computes its value.



+2


source


We don't have a source for UIKit, so we can't say definitively what is called ivar.

However, Apple's convention is similar to _ivar.

It seems that Apple has declared _navigationController (or whatever it can be called) as a private ivar using @private

, which means that the ivar can only be accessed by instances of that class, not subclasses.

Also - this property is declared read-only, so you can't even write it, which means Apple really doesn't want you to change it.

Note that you have more than two possibilities for the name ivar. The property can be represented by any name ivar. If you look at Apple's documentation they have an example:

@synthesize firstName, lastName, age=yearsOld;

      

Indicates that the accessors for firstName, lastName, and age should be synthesized, and that the age of the property is represented by the yearsOld instance variable.

+2


source







All Articles