I've been using Objective-C for a while now, and if I declare variables in the .m file I thought I had to put them inside {}
below the @implementation
but I've just come across a .m file where they haven't done that, instead they have just put something like int nn = 0;
under @implementation
(although under the @synthesize
instructions). What's going on?
Asked
Active
Viewed 96 times
0

Carl Veazey
- 18,392
- 8
- 66
- 81

Phil
- 2,995
- 6
- 40
- 67
2 Answers
4
If it's outside of the brackets then it's a global variable, regardless of whether it comes before or after the @implementation
. E.g.:
@implemenation Foo
{
int instanceVariable;
}
int globalVariable = 123;
- (void)bar
{
int localVariable;
instanceVariable = 1;
globalVariable = 2;
localVariable = 3;
}

Darren
- 25,520
- 5
- 61
- 71