Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

saurabhshukla

macrumors newbie
Original poster
Mar 20, 2009
12
0
India
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

Suspended
Nov 25, 2005
17,980
5,566
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

macrumors 6502a
Oct 26, 2003
932
8
Richmond, VA
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

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
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

macrumors newbie
Original poster
Mar 20, 2009
12
0
India
Thanks

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:)
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.