Saturday, June 23, 2012

Dot operator in Objective C

In Objective-C, dot operator can be used to access the member function of a class. The syntax makes you feel that you are directly accessing the instance variable of a class, but you are not.

You can can use dot operator to.
  1. to access the member function with zero argument and 
  2. to access the member function with one argument with no return value. Provided the name of the function starts with word 'set' and the character just after 'set' is an uppercase ascii or an underscore.
Lets see this with example

File: TestVariableAccess.m
#import "TestVariableAccess.h"

@implementation TestVariableAccess

-(int)function{
    NSLog(@"function_ret");
    return 100;
};


-(int)setFunctionWithArg:(int)arg{
    NSLog(@"function_ret withArg %d", arg);
    return 101;
};


@end


These functions can be called with the dot operator as follows.

The below code will call the method named function and the return value is assigned to the variable a.

TestVariableAccess *obj = [[TestVariableAccess alloc]init];
int a = obj.function;

The below code will call the method named setFunctionWithArg. Note that the compiler adds the word 'set' to the method name and makes the first character in the method name to uppercase before calling the method.

TestVariableAccess *obj = [[TestVariableAccess alloc]init];
obj.functionWithArg = 10;


Setters and getters

Though the dot operators can be used to call any method abiding to the above mentioned rule, it is meant to be used for setters and getters.
Consider the below declared variable value

TestVariableAccess.h
@interface TestVariableAccess : NSObject{
    int value;
}

-(int)value;
-(void)setValue:(int)v;

@end

TestVariableAccess.m
#import "TestVariableAccess.h"

@implementation TestVariableAccess

-(int)value{
    return value;
};


-(void)setValue:(int)v{
    value = v;
};


@end

Here the getter is value and the setter is setValue. In Objective-C the name of the getter is same as that on the name of the variable and the name of the setter is variable name prefixed with 'set' word with the first character in the variable (if it is not underscore) is made to upper case.

With the dot operator, the getter is called by
TestVariableAccess *aa = [[TestVariableAccess alloc]init];  
int v = aa.value;

and the setter is called by
TestVariableAccess *aa = [[TestVariableAccess alloc]init];  
aa.value = 12;

This way we can access the instance variable with dot operator through setters and getters. Though the code seems like we are accessing the instance variable directly, we are not. Instead we are accessing the variable through setters and getters.

If there is not setters and getters defined, the compiler will throw error. 

Accessing public variable directly

You can declare a public instance variable in Objective-C class as shown below.

@interface TestVariableAccess : NSObject{
@public
    int pubValue;
}
@end

Public variables can be accessed without declaring setters and getters. Since the dot operator is reserved for setters and getters, the Objective C  uses -> operator to access the public variable.

TestVariableAccess *aa = [[TestVariableAccess alloc]init]; 
        
// Setting value
aa->pubValue = 12;
        
// Getting value
int v = aa->pubValue;
        

No comments:

Post a Comment