简体   繁体   中英

Objective-C Naming Conventions - A Clarification

I have a doubt. Its not obvious for me.

Lets say we have a @property named myProperty in a class. We can do two things while synthesizing it. Either we can assign an ivar , say _someIVar , to it, or we can just simply omit it in which case the compiler takes care of creating an ivar with the same name, myProperty , for that property. In the first case we can access the property using the ivar _someIVar . In the second case we can use the ivar myProperty which was created by compiler for us.

Now my doubt is, when we push another view controller from a view controller which is already in the navigation stack, we use,

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

As navigationController is a property, I assume that it should be having an ivar assigned to it. There are only two possibilities.

1) First one is Apple has the convention of naming the ivars with an preceding underscore(_) . If this is the case I can call the pushViewController:animated: method like this,

[_navigationController pushViewController:newViewController animated:YES];

2) Second possibility, obviously, is the ivar with no underscore,

[navigationController pushViewController:newViewController animated:YES];

But the compiler is not allowing me to access the navigationController in either way. Why? Is that something related to private properties or something or I just don't understand the Objective-C at all?

You don't know what Apple did to implement the navigationController property, so you cannot just assume there is a _navigationController ivar floating in the UIViewController class somewhere. Case in point: A property can be declared @dynamic, in which case the implementation can be absolutely anything at all.

There's no way to guess at the ivar backing a property just by looking at the property itself.

You cannot guess at the name of an ivar. What's more, you should not be trying to access ivars declared in superclasses. And thirdly, there's no guarantee that the navigationController property is backed by an ivar at all, it could be backed by a custom getter that calculates its value.

We don't have the source to UIKit, so we cannot say definitively what the ivar is called.

However, Apple convention does seem to be _ivar.

What it seems like is that Apple has declared _navigationController (or whatever it may be called) as a private ivar using @private , which means that the ivar can only be accessed by instances of that class and not subclasses.

Also - that property is declared read-only, so you can't even try to write to it, meaning that Apple really doesn't want you to change it.

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

@synthesize firstName, lastName, age=yearsOld;

This specifies that the accessor methods for firstName, lastName, and age should be synthesized and that the property age is represented by the instance variable yearsOld.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM