views:

45

answers:

2
// myClass.h
@interface myClass : NSObject {
    int variable1;
}

- (int) addOne: (int)variable1;

//myClass.m
- (int) addOne: (int)variable1{
variable1++;
}

My question is: will [myClass addOne:aNumber] add 1 to aNumber or will it add 1 to the value of the ivar variable1?

+2  A: 

Local variable (or function parameter) hides instance variable declaration (you should get compiler warning about that) - so local copy of aNumber will be incremented.

Vladimir
Quick response and very helpful. Thanks!
G.P. Burdell
A: 

It will add one to aNumber in order to add one to ivar you will have to write self.variable1 += 1, I think even ++ may work.

Akash Kava
you will need to declare a property for variable1 to use that. self->variable1+=1 will work as well.
Vladimir
yeh you are right, I am not objective-c expert, i thought self.variable must do it, but yes it should be self->variable, thanks.
Akash Kava
Just to be clear, do you literally mean that `self->variable` will give you the ivar without having to declare a property for it?
G.P. Burdell
@GP, yes, self->variable will give you the ivar without having to declare property in its own class, however i dont know if objective-c has access restrictions like public or private, otherwise you can access it from anywhere, i think its like C, you can access anywhere.
Akash Kava