-3

So I am new to iOS and have been noticing that not only can you use . notation but there is : (colon).

My question is what is the difference between these two notations? what do they mean? When are they used? Are they interchangeable? When is it best to use which?

Thank you for any and all information on this matter.

Example:

[self.layer setBorderColor:[UIColor blackColor].CGColor];
self.layer.borderColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5].CGColor;

Edit:

I've even seen some instances where ':.' is used. What does that mean? (I don't have an example right now, if I find one I will update this post).

CGTheLegend
  • 734
  • 1
  • 7
  • 19
  • 1
    please show an example. – John Riselvato Aug 27 '14 at 18:15
  • Can someone upvote my question so I am not in the negative, I'm trying to build my reputation so I an have access to more features. But people don't seem to like my questions. – CGTheLegend Aug 27 '14 at 18:23
  • @CGTheLegend: People probably don't like your question because 1) its original version was bad (didn't provide enough detail) and 2) this question has been asked here sooooo many times… you'd have found your answer if you had used the search function. If you want to build up reputation put more effort into your questions (and answers). Provide more details, give examples, be specific. – DarkDust Aug 27 '14 at 18:44
  • I did search it and didn't find the general answer I was looking for. Maybe I didn't look hard enough. And it's not that I am intentionally trying to post redundant or lacking questions. But sometimes I don't realize I'm not being clear enough until someone asks me to specify, and sometimes I don't know enough when I initially write the post to ask the appropriate questions. I do try and make my posts as useful as possible so that others who in a similar position such as myself can benefit from them. – CGTheLegend Aug 27 '14 at 19:43

3 Answers3

2

Side note, : is called colon. A semicolon is ;. The one you mean is colon.

What you are encountering is called "dot notation" and is relatively new in Objective-C (2007 if I remember right). At the time, and somewhat to this day, it has been controversial because of the confusion it creates.

Before dot notation was added in Objective-C 2.0, the bracket notation was the only way to call methods, and there were no properties. So your example would have been written:

[[self layer] setBorderColor:[[UIColor blackColor] CGColor]];

This is how ObjC was written for decades. When ObjC2 came out, it added a shortcut for this called "dot notation."

self.layer.borderColor = [[UIColor blackColor] CGColor];

In theory, dot notation was only supposed to be used for properties. In practice, the compiler allows you to use it any time you have a method with the correct signature. This is part of its controversy. There are now multiple syntaxes that do the same thing, and when you use one or the other is murky. That said, dot notation is wildly popular because it reduces typing slightly (and frankly because it looks slightly more like Java, which I suspect was its reason for being).

Dot notation can be confusing because it is ambiguous with struct notation. For example, you would expect the following to work:

self.frame.origin.x = 0.0;

Unfortunately, self.frame is a method call using dot-notation, but frame.origin and origin.x are struct accesses, which are completely different and aren't compatible with dot notation (so the above won't compile). There is no way to know that from the code; you just have to know how frame works.

Very long story short, though, in most cases:

self.foo       <=> [self foo]
self.foo = bar <=> [self setFoo:bar]

The compiler just converts the former into the latter as a convenience to the programmer.

Rob Napier
  • 286,113
  • 34
  • 456
  • 610
0

Dot is for properties, ":" is just separator between argument name and value in method call.

BOOL b = control.isActive; // Getting property value
control.setIsActive = YES; // Setting property value

// The same using selectors:
BOOL b = [control isActive];
[control setIsActive:YES];

So, your example could be rewritten as:

 [[self layer] setBorderColor:[[UIColor colorWithRed:0 green:0 blue:0 alpha:0.5] CGColor]];
Vadim Kalinsky
  • 928
  • 8
  • 18
  • Why do you need the extra brackets for the second example? Can it also be represented by self.layer? – CGTheLegend Aug 27 '14 at 18:32
  • Your rewritten example is wrong, it should be: `[[self layer] setBorderColor:[[UIColor colorWithRed:0 green:0 blue:0 alpha:0.5] CGColor]]`; – DarkDust Aug 27 '14 at 18:49
0

No they are not interchangeable. One example of the '.' is when accessing properties:

myObj.someProperty = 10;

or...

int someValue = myObj.someProperty;

In this example you are accessing a property of an object, which is an instance of a class.

An example of ':' can be seen with passing parameters:

[myObj someMethod:10 secondParam:20 thirdParam:40];

In this example the value of the parameter follows the ':'. You cannot do something like:

[myObject someMethod.10 secondParam.20 thirdParam.40];

Just like you can not say:

myObj:someProperty;

These are all basics of Objective-C and I suggest studying it from the beginning with a good book or reference.

emesx
  • 12,555
  • 10
  • 58
  • 91
Allen S
  • 1,042
  • 2
  • 9
  • 14
  • I've looked at some resources for the basics but I feel I can always learn more about Objective-C. Do you have any suggestions for a good book? Preferably one I can have in digital format? – CGTheLegend Aug 27 '14 at 18:27
  • I don't know of any books off the top. I suggest checking out Amazon's Kindle section and looking at books with a large number of reviewers that also have a very high review score. – Allen S Aug 27 '14 at 19:34