何时使用括号以及何时使用 Objective-C 中的句点 [英] When to use brackets and when to use the period in Objective-C

查看:49
本文介绍了何时使用括号以及何时使用 Objective-C 中的句点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一名新的 iPhone/Objective-C 开发人员,当我浏览不同的教程和开源代码时,我在理解何时使用方括号[]"以及何时使用时遇到了一些问题使用句点."来访问对象的属性/方法.

I'm a new iPhone/Objective-C developer and as I'm going through different tutorials and open source code, I am having a bit of a problem understanding when to use the square brackets "[ ]" and when to use the period " . " for accessing properties/methods of an object.

例如这段代码:

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {

  [super setSelected:selected animated:animated];

  UIColor *backgroundColor = nil;
  if (selected){
    backgroundColor = [UIColor clearColor];
  } else {
    backgroundColor = [UIColor whiteColor];
  }

  self.todoTextLabel.backgroundColor = backgroundColor;
  self.todoTextLabel.highlighted = selected;
  self.todoTextLabel.opaque = !selected;

  self.todoPriorityLabel.backgroundColor = backgroundColor;
  self.todoPriorityLabel.highlighted = selected;
  self.todoPriorityLabel.opaque = !selected;
}

为什么[UIColor clearColor]得到括号,而todoTextLabel.backgroundColor得到句点?

Why does [UIColor clearColor] get brackets, but todoTextLabel.backgroundColor get the period?

有人可以为我简单解释一下吗?

Could someone explain this easily for me?

推荐答案

我在新代码中看到的约定是对属性使用点号,并且总是对消息/选择器使用方括号(你所谓的方法).点是在 Objective-C 2.0 中引入的,因此您在网上找到的信息不一致并不完全出乎意料.

The convention I have seen in new code is to use the dot for properties, and always use square brackets for messages/selectors (what you call methods). The dot was introduced in Objective-C 2.0, so the disagreement of information you find online is not entirely unexpected.

也完全可以将方括号用于所有内容,仍然(我这样做):

It's also entirely possible to use square brackets for everything, still (and I do):

foo = [myObject backgroundColor];
[myObject setBackgroundColor:foo];

相当于

foo = myObject.backgroundColor;
myObject.backgroundColor = foo;

重申一下,您不应该将点用于消息,而应仅用于属性.

To reiterate, you should not be using the dot for messages, only properties.

要回答您的具体问题,[UIColor clearColor] 属于括号,因为它不是属性;它实际上是 UIColor (+(UIColor)clearColor) 的类消息.

To answer your specific question, [UIColor clearColor] belongs in brackets because it is not a property; it's actually a class message to UIColor (+(UIColor)clearColor).

您听起来像是来自 Java 世界,所以这可能会有所帮助:

You sound like you come from a Java world, so this might be helpful:

MyObject *foo = [[MyObject alloc] initWithAwesome:YES];    /* MyObject foo = new MyObject(TRUE); */
[foo doSomethingWithNumber:5 andString:"five"];            /* foo.doSomething(5, "five"); */
MyColor *bar = foo.faceColor;                              /* MyColor bar = foo.faceColor; */
MyColor *baz = [foo faceColor];                            /* MyColor baz = foo.faceColor; */
foo.backColor = bar;                                       /* foo.backColor = bar; */
[foo setUndersideColor:baz];                               /* foo.undersideColor = baz; */

setXXX"和XXX"消息来自合成的动态属性,是一个Objective-C习惯用法.点"只是调用这些方法的简写,大致等效.

The "setXXX" and "XXX" messages come from synthesized dynamic properties, and are an Objective-C idiom. The "dot" is simply a shorthand for calling those methods, and is roughly equivalent.

现在我得到了一些赞成票,是时候让你们中的一些人重新考虑>:)

Now that I've got some upvotes, time to make some of you reconsider >:)

我从不使用点,你也不应该使用.

I never use dots, and neither should you.

这篇关于何时使用括号以及何时使用 Objective-C 中的句点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆