Pergunta

I taught myself C and C++ and am trying to learn Objective-C now, but I am a little confused by the dot operator used on instances of classes. Say I declared a class:

MyClass* myinstance = [[MyClass alloc] init];

//Then I call the member function foo:

myinstance.foo;

Obviously this works in Objective-C, but in C++ or C (in the case of a struct), this wouldn't. You would have to use the operator ->. So I'm looking for an explanation of what exactly the .(dot) operator does in Objective-C and how the two different meanings from ObjC and C don't cause compatibility issues between C and Objective-C even though Objective-C is a strict superset of C.

Foi útil?

Solução

Dot notation is to call the method as we do it by using space. we can call methods by using space with all variables but dot notation is used with property and synthesizes variables only

example

 variable.method_name; //is a dot notation with property and synthesized variable
[variable method_name]; // is space notation

and the dot notation is for all default properties because those are already property and synthesized like

label.text = @"Ashok";

and as said space can be used by synthesizing separately using set method as

[label setText:@"Ashok"];

ThanQ

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top