Global Variable

Discussion in 'iOS Programming' started by iphonejudy, Mar 19, 2009.

  1. iphonejudy macrumors 6502

    iphonejudy

    Joined:
    Sep 12, 2008
    #1
    Hi,


    I need to display a global variable.I checked with some documents.But it is not clear.Can anyone specify a good document for global variable
     
  2. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    Global variables are bad. Don't use them. Either add the variable as a property to your app delegate (so it's easily accessible from everywhere else) or create a singleton class that will hold it in your data model...
     
  3. RobertBlackburn macrumors newbie

    Joined:
    Oct 7, 2008
    #3
    like robbieduncan says you really should avoid using global variables but when i really do need to use them (normally when i am being lazy) i just make a header file and put the global variable in there then import the header into the classes i need to use the variable in
     
  4. firewood macrumors 604

    Joined:
    Jul 29, 2003
    Location:
    Silicon Valley
    #4
    Global variables are great. They are the original variable type, from back in the days when Fortran and Basic were first designed. They are both fastest and easiest way to access singleton data and (after initialization) objects across an entire app. Just be sure you only will ever want to have one, and don't mind being able to access and clobber the value from anywhere in your program.

    There are several problems with using globals, but many small quick apps are just not meant to be scalable or reusable. No reason to get out the artillery to squash a moth on the wall. You don't need FDA and OSHA approval to whip up dinner using vegetables from your backyard.

    imho.
     
  5. mccannmarc macrumors 6502

    mccannmarc

    Joined:
    Aug 15, 2008
    Location:
    Manchester, UK
    #5
    Putting global variables into your AppDelegate is structurally just as bad as using global variables in a header, besides its much easier to access variables using a header than having to type [[UIApplication sharedApplication].delegate GlobalVariable] every time you want to access it!

    If have have a lot of global variables and don't want to end up with a complete unstructured mess of a program then you should definitely go the route of a singleton class to hold them all although personally I see no harm in using global variables in a global header file unless your code is going to be used by others.
     
  6. firewood macrumors 604

    Joined:
    Jul 29, 2003
    Location:
    Silicon Valley
    #6
    Global variables are not structurally bad, in and of themselves. For instance, I would call Knuth's examples in MIX assembly language well structured. There were books on developing well structured Cobol and Fortran applications well before the '70's fad of structured programming (in Pascal usually).

    It is true that the use of global variables does not scale well, but most iPhone developers don't work for an intergalactic bank (or somesuch) where some clueless 3-headed manager will demand that we take Obj-C code from some 120 year old iPhone app and scale it to work reliably for gazillions of simultaneous touches from our new 73-fingered Jovian customers.

    imho.
     
  7. mccannmarc macrumors 6502

    mccannmarc

    Joined:
    Aug 15, 2008
    Location:
    Manchester, UK
    #7
    I was defending the use of global variables. I was trying to say that if using global variables is bad then so is putting variables into your appdelegate as its just as unstructured. I personally use global variables a lot and I agree with everything you say but I am not going to advertise that they are good programming practise as they certainly are not
     
  8. eddietr macrumors 6502a

    Joined:
    Oct 29, 2006
    Location:
    Virginia
    #8
    Actually, encapsulating the variable in a class or in a singleton of a class is still better than having it floating out there is global scope, for a couple different reasons.

    But of course to have a true "enforced", if you will, singleton in NS/Cocoa does actually require you to use a global variable. (Unless there is a newer technique I'm not aware of)
     
  9. firewood macrumors 604

    Joined:
    Jul 29, 2003
    Location:
    Silicon Valley
    #9
    And encapsulating the variable in a class makes the names of the getters and setters visible to anyone who runs "strings" on your unzipped bundle's binary (as well as making global access slower, the code larger and more likely to miss the cache, yada yada...) The value of encapsulation is not a Boolean.

    imho.
     
  10. eddietr macrumors 6502a

    Joined:
    Oct 29, 2006
    Location:
    Virginia
    #10
    Sure, we're not talking about tools to provide some sort of cryptographic protection for instance variables. :)

    We're just talking about the normal issues in software development like collision and reuse issues. If your code needs to be reconfigured or reused in a new architecture, polluting the global namespace hurts.

    Not to mention when you talk about scaling your code onto multiple cores or multiple machines. Having gates to the access to variables (even if those gates are wide open) is actually very useful in practice.

    Hence the global statement that globals suck. But in our case (with Cocoa), since we don't have class variables, we end up having to use globals even just to have singletons.
     
  11. matt87305 macrumors newbie

    Joined:
    Apr 4, 2007
    #11
    help with global variables / singleton usage

    I am trying to develop a fairly simple app (or so i thought) haha. The idea is that the user clicks a UIButton and through the MessageUI framework, the app opens an email with everything filled out (to, subject, body, etc). the whole point of this email feature is so that the user can share their location (latitude and longitude coordinates) via sending it in a pre-filled out email. I know how to get the lat / long coords in string form, and i know how to open the email with the pre-filled-out stuff EXCEPT for how to insert the lat / long strings into the body portion of the email. it's like, i need to get the variable out of one class, and use it in another (since the lat / long values are used in the CLLocationManager class and the MessageUI framework implements another class in which it prepares the email for sending). All my googling seems to turn up two things: global variables, and singleton usage. Although everyone seems to be hating on the global variables, I only need to use TWO, one for latitude and one for longitude, that i can call whenever i want in the program no matter where i am. any help / suggestions would be immensely appreciated. thanks!

    matt
     
  12. firewood macrumors 604

    Joined:
    Jul 29, 2003
    Location:
    Silicon Valley
    #12
    You can also use notifications to broadcast data sideways across an object hierarchy.

    The MVC pattern might indicate that a model controller that relates the (network diagram) combination of a particular button to a particular email would be the appropriate place for their shared model state. If you had thousands of different buttons potentially connected to thousands of different emails under composition, the solution criteria would be different.

    But for only 1 button and 1 email, that might be overkill. For a small amount of data in a small app (and with small classes you don't plan on reusing in another app), using a few global variables is faster and generates less code.
     

Share This Page