0

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?

Carl Veazey
  • 18,392
  • 8
  • 66
  • 81
Phil
  • 2,995
  • 6
  • 40
  • 67

2 Answers2

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
0

Since you have no code to show, it is hard to say. Technically, any valid C will compile in Obj-C. Since nn is an int, this would be perfectly valid c.

Undo
  • 25,519
  • 37
  • 106
  • 129
aaronman
  • 18,343
  • 7
  • 63
  • 78