Quote:
Originally Posted by iphone.savvy
HI EXPERTS,
I have a class A in which i have declared some variable 'var' in the header file and have property for that Variable.
In Class B i have an instance of Class A and through which i am calling 'var' .
Calling variable like below,
aClassObject.var , it gives me an error , but in many ways i noticed variable calling using Dot.
And When i call as follows,
[aObject var] No ERROR and NO WARNING.
This is confusing me call the messages using square brackets but it is also calling variable.
Can anyone of you just put some light on this?
|
You need to read up on properties.
When you declare an @property in your header, you are telling the system that you will create a setter and a getter method that can be used to read or write values to the instance variable that is used to save that property's variable.
When you add an @synthesize statement, the compiler generates a setter and a getter method for your property:
the getter
The setter:
When you use code like this:
Code:
anotherVar = anObject.var;
The compiler recognizes this as a property reference. If you haven't declared a property, the compiler generates an error.
The statement
Code:
anotherVar = [anObject var];
is an explicit call to the getter method.
The compiler will generate a WARNING (not an error) if it can't find that method. That's because method calls are resolved at runtime. If there is a method with that name at runtime, the code will work. If not, it will crash.
In order for class A to refer to a property of class B, the .m file for class A needs to #import the header of class B. My guess is that you forgot that step, and that you actually got a warning when you first compiled your code. If you select clean from the project menu and rebuild, I bet you'll see a warning about an unknown method for that call.