Reputation: 3071
I am trying to understand Referring to Instance Variables from Apple guide but having issue understudying this, Apple Doc says
...When the instance variable belongs to an object that’s not the receiver, the object’s type must be made explicit to the compiler through static typing. In referring to the instance variable of a statically typed object, the structure pointer operator (->) is used. Suppose, for example, that the Sibling class declares a statically typed object, twin, as an instance variable:
@interface Sibling : NSObject
{
Sibling *twin;
int gender;
struct features *appearance;
}
As long as the instance variables of the statically typed object are within the scope of the class (as they are here because twin is typed to the same class), a Sibling method can set them directly:
- makeIdenticalTwin
{
if ( !twin )
{
twin = [[Sibling alloc] init];
twin->gender = gender;
twin->appearance = appearance;
}
return twin;
}
Upvotes: 0
Views: 100
Reputation: 21221
Referring to instance variable means, accessing the class instance vars
For example:
@interface ClassA : NSObject
{
int value;
}
- (void) setValue:(int) val;
@implementation ClassA
- (void) setValue:(int) val
{
//here you could access class a value variable like this
value = val;
}
Now accessing other classes variables take for example this class
@interface ClassB : ClassA
{
ClassA aClass;
}
- (void) setValueInAClass:(int) val;
@implementation ClassB
- (void) setValueInAClass:(int) val
{
//class b could access variables from class a like this
aClass->value = val;
}
Please note that this is very un recommended to do, using the "->" breaks the encapsulation of class a, so dont in 99% of the cases referring to class variables using the "->" is a mistake
Upvotes: 1