I hope this question is not too basic for someone to help me out on.
I have a variable who’s value I define in one method that I would like to use and manipulate in another. Is this possible?
I hope the simple expample code attached will help.
I want the value for ‘c’ to be 3, but it is only 2.
int a = 0;
-(void)method1 {
int a = 1;
NSLog(@"method 1--> a = %d", a);
}
-(void)method2 {
int b = 2;
NSLog(@"method 2--> b = %d", b);
int c = a + b;
NSLog(@"method 2--> c = %d", c);
}
int a = 1 in method1 declares new local variable distinct from ‘a’ declared globally. If you want global ‘a’ to be used here – omit ‘int’ here.This will turn declaration of local variable ‘a’ with initialization into assignment to the globally declared ‘a’.