简体   繁体   English

iPhone-这两行代码之间的区别

[英]iPhone - difference between these two lines of code

What is the diffrence between these two lines of code: 这两行代码之间的区别是什么:

[cmController currentPageNo];
self.cmController.currentPageNo;

There is actually a functional difference - the second line is equivalent to: 实际上存在功能上的差异-第二行等效于:

[[self cmController] currentPageNo];

Using the -cmController property getter method to access the cmController ivar may have behaviours that make it different to accessing the ivar directly. 使用-cmController属性的getter方法访问cmController ivar可能会导致其行为与直接访问ivar不同。 These might include lazy initialisation or atomic thread locking, or a range of other behaviours. 这些可能包括延迟初始化或原子线程锁定,或其他一系列行为。 Unless you have a good reason to, you should (in general) use the accessor methods for properties, rather than accessing ivars directly. 除非您有充分的理由,否则(通常)应该使用属性的访问器方法,而不是直接访问ivars。

So to clarify: 所以要澄清一下:

[cmController currentPageNo]; // 1
cmController.currentPageNo;   // 2

[[self cmController] currentPageNo]; // 3
self.cmController.currentPageNo;     // 4

1 and 2 are functionally identical to each other, but use different syntax. 1和2在功能上彼此相同,但是使用不同的语法。 3 and 4 are also functionally identical to each other, but use different syntax. 3和4在功能上也相同,但使用不同的语法。 You should use version 4, or version 3 if you have an aversion to dot-syntax. 如果您不喜欢点语法,则应使用版本4或版本3。

Generally, dot notation is just syntactical sugar. 通常,点表示法只是语法糖。

But in this case, there actually is some difference. 但是在这种情况下,实际上存在一些差异。

[cmController currentPageNo];

This will use the class's instance variable directly to get your cmController. 这将直接使用类的实例变量来获取您的cmController。 It will then send the currentPageNo to it. 然后它将发送currentPageNo到它。

self.cmController.currentPageNo;

This, on the other hand, will use the current class's property definition to get the cmController via the class's ivar. 另一方面,这将使用当前类的属性定义通过该类的ivar获取cmController。

This means, basically, the the first is slightly more efficient than the second. 基本上,这意味着第一个比第二个稍微有效率。 Not because you used dot notation, but because you used cmController directly instead of self.cmController . 不是因为您使用了点符号,而是因为您直接使用了cmController而不是self.cmController [whatever currentPageNo] is the same as whatever.currentPageNo . [whatever currentPageNo]相同whatever.currentPageNo

These lines are equivalent except for the syntax used: 这些行除了所使用的语法外是等效的:

[cmController currentPageNo];
cmController.currentPageNo;

On the other hand, these lines are equivalent except for the syntax used: 另一方面,除了所使用的语法外,这些行是等效的:

self.cmController.currentPageNo;
[[self cmController] currentPageNo];

I hope that's clear. 我希望这很清楚。

You can read about dot notation here . 您可以在此处阅读有关点符号的信息

There's no difference, functionally; 功能上没有区别; objective C 2.0 just introduced the dot notation, giving those who aren't as into the brackets a way out. 目标C 2.0刚刚引入了点表示法,为那些不太喜欢括号的人提供了出路。

EDIT: 编辑:

As the comments pointed out, of course this is wrong. 正如评论所指出的,这当然是错误的。 I was responding to the (unasked) question of "what's the difference between [cmController currentPageNo]; and self.cmController.currentPageNo; ?" 我正在回答(未问)“ [cmController currentPageNo];self.cmController.currentPageNo;之间有什么区别?”的问题

Nick's answer gives the true answer, which is that the first line directly accesses the variable, while the second line uses the variable's getter method. Nick的答案给出了正确的答案,即第一行直接访问变量,而第二行使用变量的getter方法。 The two lines also differ in that the first uses typical bracket notation, while the second does indeed use the new dot notation from objective C 2.0. 这两行的不同之处还在于,第一行使用典型的括号符号,而第二行确实使用了来自目标C 2.0的新点符号。

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

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