Objective-C IPhone Xcode Global Variable with extern

Discussion in 'Mac Programming' started by springframework, Mar 31, 2008.

  1. macrumors member

    Joined:
    Mar 31, 2008
    #1
    so i made a header file Global.h

    PHP:
    extern int gVar;
    inside main.m

    PHP:
    int gVar;

    int main( ... ){
    gVar 5;
    ... 
    // more code
    }
    inside another .m file

    PHP:
    #include Global.h
    gVar 88;
    inside yet another .m file

    PHP:
    #include Global.h
    NSLog( ... , gVar); // expected output 88

    This wont compile ....

    By simply taking out "extern", it works with expected output of 88.

    My variable seems to be global without using extern?



    Is my variable truely global? it has to be ... it prints out 88.


    Does anyone know a better way to do global variables?

    I plan on having NSMutableArray/NSXMLDocument global variables. will the way im doing it work with these more advanced data structures?
     
  2. macrumors 6502

    Monkaaay

    Joined:
    Jun 19, 2006
    Location:
    Richmond, VA
    #2
    There was a thread on this recently. I can't find it at the moment. Essentially, extern wasn't the way to do it.
     
  3. thread starter macrumors member

    Joined:
    Mar 31, 2008
    #3
    can you post the link the the thread if you find it, i searched the forum but all posts used extern.

    Also i just tried a NSMutableAray in the global.h and it worked as expected.
     
  4. macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #4
    http://forums.macrumors.com/showthread.php?t=459249

    I suggested using functions to access and modify a global variable declared in a single file, and those functions can be used to get at this variable. This way if you need to change the variable name or something like that, it's a single file to be changed.

    -Lee
     

Share This Page