简体   繁体   English

Objective-C命名约定 - 澄清

[英]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. 比方说,我们有一个名为类myProperty的 @property。 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. 我们可以为它分配一个ivar ,比如_someIVar ,或者我们可以简单地省略它,在这种情况下,编译器负责为该属性创建一个名为myPropertyivar In the first case we can access the property using the ivar _someIVar . 在第一种情况下,我们可以使用ivar _someIVar访问该属性。 In the second case we can use the ivar myProperty which was created by compiler for us. 在第二种情况下,我们可以使用由编译器为我们创建的ivar myProperty

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. 由于navigationController是一个属性,我认为应该为它分配一个ivar There are only two possibilities. 只有两种可能性。

1) First one is Apple has the convention of naming the ivars with an preceding underscore(_) . 1)第一个是Apple具有使用前面的下划线(_)命名ivars的约定。 If this is the case I can call the pushViewController:animated: method like this, 如果是这种情况我可以调用pushViewController:animated:这样的方法,

[_navigationController pushViewController:newViewController animated:YES];

2) Second possibility, obviously, is the ivar with no underscore, 2)显然,第二种可能性是没有下划线的ivar,

[navigationController pushViewController:newViewController animated:YES];

But the compiler is not allowing me to access the navigationController in either way. 但是编译器不允许我以任何一种方式访问​​navigationController。 Why? 为什么? Is that something related to private properties or something or I just don't understand the Objective-C at all? 这是与私人财产有关的东西还是我根本不理解Objective-C?

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. 你不知道Apple为实现navigationController属性做了什么,所以你不能只假设在某个地方有一个漂浮在UIViewController类中的_navigationController。 Case in point: A property can be declared @dynamic, in which case the implementation can be absolutely anything at all. 例证:一个属性可以声明为@dynamic,在这种情况下,实现绝对可以是任何东西。

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. 更重要的是,你不应该试图访问超类中声明的ivars。 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. 第三,不能保证navigationController属性完全由ivar支持,它可以由计算其值的自定义getter支持。

We don't have the source to UIKit, so we cannot say definitively what the ivar is called. 我们没有UIKit的来源,因此我们无法明确地说出ivar的名称。

However, Apple convention does seem to be _ivar. 但是,Apple惯例确实似乎是_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. 看起来Apple似乎已经使用@private将_navigationController(或其他任何可能被称为)声明为私有ivar,这意味着只能通过@private的实例而不是子类来访问ivar。

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. 此外 - 该属性被声明为只读,因此您甚至无法尝试写入它,这意味着Apple真的不希望您更改它。

Note that you have more than two possibilities for ivar name. 请注意,ivar名称有两种以上的可能性。 A property can be represented by an ivar of any name. 属性可以用任何名称的ivar表示。 If you look at the Apple documentation, they have an example: 如果您查看Apple文档,他们有一个示例:

@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. 这指定应合成firstName,lastName和age的访问器方法,并且属性age由实例变量yearsOld表示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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