0

I am New in Objective-c
During Coding I am Come across code like self->address and self.name
1)what is -> and . (they are operator or something else)
2)what is difference between Them
3)how to decide which one is used

If possible then please give small example

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Sangram Shivankar
  • 3,535
  • 3
  • 26
  • 38

3 Answers3

1

what is -> and . (are they operators or something else)?

Both -> and . are operators, of the same (highest) precedence.

what is the difference between them?

-> takes a pointer+field; . takes a struct+field, or a pointer+property that has an accessible getter.

how to decide which one to use?

This is implied by the sides of the operation, in the sense that, in the absence of name collisions, you wouldn't get into a situation when both operators could be used without causing a compile-time error. When you have a pointer and a field, use -> operator; otherwise, use .

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • 1
    Thanks dasblinkenlight for your answer and quick reply – Sangram Shivankar Feb 29 '16 at 18:58
  • If you have a property and an accessible instance variable with the same name, both operators will work, but the results are different. You should also have no reason to use the `->` operator with Objective-C classes, though it does come up in rare cases. – Avi Feb 29 '16 at 19:05
  • @Avi That's precisely the reason why I said "in the absence of name collisions". – Sergey Kalinichenko Feb 29 '16 at 19:06
  • @Avi As far as suggesting that there should be no reason to use `->` goes, I am reluctant to do so, for the same reason that I stay away from discussions of individual statements and operators in programming languages. – Sergey Kalinichenko Feb 29 '16 at 19:10
1

If you have object.name you are calling a method called name on object. This method may access an instance variable that it then returns, but it is a method, so it can do whatever it is written to do. Objective-C Synthesises methods for properties, so you may not have actual Objective-C code, but the methods still exist and can be overridden.

With object->name you are access the instance variable name of object. This is a direct access to the memory of object thus there is no method call and nothing to implement / override.

In general, for object's, you should use object.name to access the property unless there is a specific reason no to do this.

As a note, when you reference an instance variable inside of an object itself it is dereferencing self implicitly, i.e. name is the same as self->name.

hypercrypt
  • 15,389
  • 6
  • 48
  • 59
0

. is an operator used to access members of a struct. However, in Objective-C, it is also used to access properties of a class (via accessors). So, in your example, self.name is used to access a property called name in your class.

-> is a dereference operator from C. It is used to access members of a struct via a pointer to it. In Objective-C, it can also be used to access members of a class. In your example, self->address is used to access a member variable (so called ivar) named address in your class.

Take a look at this answer.

Community
  • 1
  • 1
0xDEADBEEF
  • 83
  • 5