G.P. Burdell
G.P. Burdell

Reputation: 161

Scope of parameter names in Objective C methods

// 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?

Upvotes: 1

Views: 147

Answers (2)

Akash Kava
Akash Kava

Reputation: 39916

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.

Upvotes: 0

Vladimir
Vladimir

Reputation: 170839

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

Upvotes: 3

Related Questions