1

In Objective-C i saw that we can use dot operator to set and get a value and for the same task i saw something like "[ob method]" inside square braces method call, what do we call for this kind of syntax?

Graham Bell
  • 1,139
  • 1
  • 14
  • 30

4 Answers4

4

Bracket notation: A staple of the small talk language, and now a lovely visage of the ObjC language.

Brackets were SmallTalk's way of saying "You there, take this message and do something with it", and so that's how they were implemented in Objective C. We send a message to the first part [Object] and state the message in the second part [Object Message];

Of course, they also serve a similar function with properties. Properties in most languages are written in dot notation (Object.property), but with Objective-C and the modern runtime's support for non-ivar-backed properties, and the @synthesize directive, properties automatically generate getters named the same. Sounds complicated? It isn't. If I have the property example, then I can access it in one of two ways:

self.example; 

Or

[self example];

Easy!

But the @synthesize directive doesn't stop there. We get a getter, and a setter as well. The setter can be accessed the same number of ways as a getter.

self.example = foo;

is the equivalent of

[self setExample:foo];

Bracket notation is in fact so important, that the compiler optimizes most dot notation out to bracket notation at compile time.

CodaFi
  • 43,043
  • 8
  • 107
  • 153
1

This is called messaging, or message sending, you are sending a message method to the object ob

Its similar to calling a method in java or C++

So the equivalent in java would be

ob.method();
Omar Abdelhafith
  • 21,163
  • 5
  • 52
  • 56
0
[ob method]

It's the way you call methods in objective-c. The same way you would call myObject.method(), in Java por example.

ob.myProperty

Is how you access ivars by using it's getter/setter method, for example:

Get method -> NSLog(@"%@",ob.myProperty);

Set method -> ob.myProperty = @"Hello World";

Notice that you also use the set method like this:

[ob setMyProperty:@"Hello World"];

Or use the get method like this:

NSLog(@"%@",[ob myProperty]);

In objective-c you normally will not create manually the setter and getter, because you have the opportunity to create them using @property and @synthesize.

Rui Peres
  • 25,741
  • 9
  • 87
  • 137
-1

This is known as function calling..

like you call function in other programing language like in java or c#:

ob.method() // where ob is object and method is the function name.. 

similarly if you want to call a function in objective c the syntax is calling function is like this :

 [ob method];
Abhishek
  • 2,255
  • 1
  • 13
  • 21