Static Variable For Cocoa Application!!!!!!!

Discussion in 'Mac Programming' started by saurabhshukla, Mar 20, 2009.

  1. macrumors newbie

    saurabhshukla

    Joined:
    Mar 20, 2009
    Location:
    India
    #1
    Hi, I am developing a Cocoa Application using XCode. I want to declare a shared Variable(Like static) for whole application(Has Many Classes) so that i can change & get its value anywhere in application(in any class).

    i am declaring a static variable in one class (Before implementation @ Class Name)
    but its declaring it only for that specific class not for whole application.
    How can i do this ??? Thanks To Help
     
  2. macrumors G5

    gnasher729

    Joined:
    Nov 25, 2005
    #2
    How would you do it in C?
     
  3. macrumors 6502a

    GorillaPaws

    Joined:
    Oct 26, 2003
    Location:
    Richmond, VA
    #3
    It sounds like you're trying to break the encapsulation principles that Cocoa apps are built around. Essentially, you're trying to do this the wrong way. The "right" way to do what you're trying to do is to declare that variable for your class and then to write setter and accessor methods so any other object has access to that variable (indirectly) by calling those methods.

    There are ways to do what you want to do directly, but it's really the wrong way to go unless you've got a really important reason. I'm still a novice at this stuff so it's possible I'm wrong.
     
  4. macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #4
    This is true, but i would say that if you HAVE to do this, the OOPiest way is to have a static member of a class. Except Objective-C doesn't let you do this like, say, Java does. In java you'd have something like:
    com.me.util.myStaticVariable

    It would be a static member of the class util in the package com.me, and it could be accessed from anywhere. In Objective-C you could declare a static variable OUTSIDE of your class, but in it's .m file. You'd then write an accessor and mutator that are class(+) methods to access that thing. It's still sort of ugly, but it's OOPier than the OP's original intent.

    -Lee
     
  5. thread starter macrumors newbie

    saurabhshukla

    Joined:
    Mar 20, 2009
    Location:
    India
    #5
    Thanks


    Hi buddy thanks to help me. i was really breaking the encapsulation principle but using getter & setter my problem got solved. Thanks again:)
     

Share This Page