Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use brackets and when to use the period in Objective-C

Tags:

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.

For example, this code:

- (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;
}

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

Could someone explain this easily for me?

like image 281
James P. Wright Avatar asked Oct 15 '09 15:10

James P. Wright


People also ask

What do brackets mean in Objective-C?

It's actually calling a method named caption. In most cases, you don't add the "get" prefix to getters in Objective-C. Whenever you see code inside square brackets, you are sending a message to an object or a class.

Why we use this bracket?

Brackets (parentheses) are punctuation marks used within a sentence to include information that is not essential to the main point. Information within parentheses is usually supplementary; were it removed, the meaning of the sentence would remain unchanged.


5 Answers

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];

is equivalent to

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

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

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).

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; */

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.

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

I never use dots, and neither should you.

like image 175
Jed Smith Avatar answered Oct 05 '22 19:10

Jed Smith


I use dot notation for properties because,

for ( Person *person in group.people){ ... }

is a little easier to read than

for ( Person *person in [group  people]){ ... }

in the second case readability is interupted by putting your brain into message sending mode, whereas in the first case it is clear you are accessing the people property of the group object.

I will also use it when modifying a collection, for instance:

[group.people addObject:anotherPerson];

is a bit more readable than

[[group people] addObject:anotherPerson];

The emphasis in this case should be in the action of adding an object to the array instead of chaining two messages.

like image 42
rebo Avatar answered Oct 05 '22 18:10

rebo


Big Nerd Ranch has some thoughts on the dot notation that are worth reading.

There's also a rebuttal.

like image 27
nall Avatar answered Oct 05 '22 18:10

nall


By (strong) convention, property accessors are written as methods named after the instance variable for the getter, and the (capitalised) instance variable name prefixed with "set" for the setter (so for instance variable foo you'd have foo and setFoo).

As others have pointed out, as of Objective-C 2.0, if you write object.foo, it will map onto method call [object foo] if getting or [object setFoo: arg] for setting. You can use either form, and some people continue to prefer the full method syntax even when using Objective-C 2.0 exclusively.

A separate, but related, addition to Objective-C 2.0 are the @property/@synthesize/@dynamic keywords for generating the getters and setters. You can mix and match these with dot notation - one does not require the other.

like image 23
philsquared Avatar answered Oct 05 '22 19:10

philsquared


Yep, I just started learning this too:

Unlike other languages, syntax to invoke a method on an object is not objName.someMethod();

It's [objName someMethod]

The dot operator is used to either get or set the value of a property in a class. It's a short way of doing something.

The dot operator, as I have seen it, is always used on an instance of an object whereas the [...] can be used on either an instance of an object or statically (using the class name).

todoTextLabel can use the [] also but using the dot operator is just shorter hand...otherwise you would have to provide parameters, etc, and that's just longer notation.

Hope this helped.

like image 28
vinnybad Avatar answered Oct 05 '22 20:10

vinnybad