Objective c 在Objective C中设置属性格式的最佳方法

Objective c 在Objective C中设置属性格式的最佳方法,objective-c,shorthand,Objective C,Shorthand,我用objective c编写代码已经有一段时间了,但我仍然时常想知道是否有一种最好或最快的方法来做某事 例如,假设我有一个名为myLabel的UILabel和名为mainView的UIViewController,设置它的最快或最好的方法是什么 方法1 mainView.myLabel.text = @"String"; //similar to c/c++ style mainView.myLabel.text = @"String"; //similar to c/c++ style

我用objective c编写代码已经有一段时间了,但我仍然时常想知道是否有一种最好或最快的方法来做某事

例如,假设我有一个名为myLabel的UILabel和名为mainView的UIViewController,设置它的最快或最好的方法是什么

方法1

mainView.myLabel.text = @"String"; //similar to c/c++ style
mainView.myLabel.text = @"String"; //similar to c/c++ style
方法2

[mainView.myLabel setText:@"String"]; //between c/c++ and obj-c style
[mainView.myLabel setText:@"String"]; //between c/c++ and obj-c style
方法3

[[mainView myLabel] setText:@"String"]; //obj-c style
[[mainView myLabel] setText:@"String"]; //obj-c style

我个人更喜欢方法2,因为方法1不是一个访问它的速记方法吗?感谢您的帮助。

您列出的所有3个选项都会导致调用相同的代码(myLabel和setText:methods),因此绝对没有性能差异


因此,最好的方法是选择您最喜欢的单个选项,并在整个项目中始终如一地使用它。

您列出的所有3个选项都会调用相同的代码(myLabel和setText:methods),因此绝对没有性能差异


因此,最好的方法是选择您最喜欢的单一选项,并在整个项目中始终如一地使用它。

它们编译成完全相同的代码。没有任何效率惩罚。用你喜欢的


选项1涉及最少的键入。选项3最能描述实际发生的情况(例如,您不能将属性访问误认为是结构元素访问)。

它们编译成完全相同的代码。没有任何效率惩罚。用你喜欢的


选项1涉及最少的键入。选项3最能描述实际发生的情况(例如,您不能将属性访问误认为是结构元素访问)。

我添加了另一种方法,仅供参考,所有同行都说,所有这些都是相似的,没有性能问题

方法1

mainView.myLabel.text = @"String"; //similar to c/c++ style
mainView.myLabel.text = @"String"; //similar to c/c++ style
方法2

[mainView.myLabel setText:@"String"]; //between c/c++ and obj-c style
[mainView.myLabel setText:@"String"]; //between c/c++ and obj-c style
方法3

[[mainView myLabel] setText:@"String"]; //obj-c style
[[mainView myLabel] setText:@"String"]; //obj-c style

我添加了另一种方式,仅供参考,所有同行都说,所有这些都是相似的,没有性能问题

方法1

mainView.myLabel.text = @"String"; //similar to c/c++ style
mainView.myLabel.text = @"String"; //similar to c/c++ style
方法2

[mainView.myLabel setText:@"String"]; //between c/c++ and obj-c style
[mainView.myLabel setText:@"String"]; //between c/c++ and obj-c style
方法3

[[mainView myLabel] setText:@"String"]; //obj-c style
[[mainView myLabel] setText:@"String"]; //obj-c style

我提到了第三个选项:),但阿罗索里诺知道所有的3个选项。@AnoopVaidya在编辑之前,原始问题中有3个选项;)是的。。。这是错误的,有人纠正了它,然后为什么它被删除?我感到很自豪,我正在列表中添加一个缺少的方法。。。我提到了第三个选项:),但arosolino知道所有的3个选项。@AnoopVaidya在编辑之前,原始问题中有3个选项;)是的。。。这是错误的,有人纠正了它,然后为什么它被删除?我感到很自豪,我正在列表中添加一个缺少的方法。。。英雄联盟