PDA

View Full Version : Static Variable For Cocoa Application!!!!!!!




saurabhshukla
Mar 20, 2009, 10:30 AM
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



gnasher729
Mar 20, 2009, 11:07 AM
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

How would you do it in C?

GorillaPaws
Mar 20, 2009, 11:09 AM
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

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.

lee1210
Mar 20, 2009, 11:24 AM
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.

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

saurabhshukla
Mar 21, 2009, 04:16 AM
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

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.


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