So about Singeltons and Global ivars.

Discussion in 'iOS Programming' started by larswik, Apr 21, 2012.

  1. macrumors 68000

    Joined:
    Sep 8, 2006
    #1
    So until the other day I thought my objects instance variables were "Global Variables". My thinking was that if I had 2 or more Methods that needed to access the same variable, I would declare them "Globally" and instantiate them. If not I would declare them locally within the Method for temporary use. (like I learned in my Pascal class regarding the Scope or variables).

    - Are object instance variables also know as global variables for that object? I seem to use them in that capacity and they function that way. But are they know as Global Variables too?

    I have not learned how to use Singletons yet but I guess they store information "Globally" in my root controller which I can use to pass information to my different objects?

    - Are there any good tutorials out there to learn how to use Singletons? I did google it but sometimes folks here have good resources too. This is my weekend project to learn.

    Thanks!
     
  2. macrumors 68000

    Joined:
    Jan 28, 2003
    Location:
    Less lost in L.A.
    #2
    Based on your question, Singletons, AppDelegates and top-level data looks like a good read.

    For more put the following into Google: using singleton cocoa

    Instance variables are global to an object, yes. It's that scope thing you mentioned.

    Sometimes you'll find a variable defined above the @interface. Those variables are global but typically considered class variables.

    For some more check out Objective-C Variable Scope and Storage Class.
     
  3. thread starter macrumors 68000

    Joined:
    Sep 8, 2006
    #3
    Cool, thanks for the links. I will read up on them.

    So instance variables are also know as global variables, to that object. Thanks for the clarification.
     
  4. macrumors 68000

    Joined:
    Jan 28, 2003
    Location:
    Less lost in L.A.
    #4
    Yes, but nobody refers to them as global to the object because global indicates more than that. People refer to them as instance variables, or ivars in short form.
     
  5. thread starter macrumors 68000

    Joined:
    Sep 8, 2006

Share This Page