I am writing a class that has many class variables. So I am declaring the variables as static in my .m file and before @implementation
statement, with setters and getters for them as class methods. Is it a good idea to do this for lets say more than 10 class variables? Or is there a better alternative to do this?

- 3,243
- 13
- 56
- 93
-
2Why don't you describe the problem you are trying to solve with such a class and lots of class methods. Without more info about what you are trying to do, it will be hard to get a good answer. And be sure to put the extra details in your question and not in comments. – rmaddy Aug 11 '14 at 18:07
-
Asking questions without accepting answers: bad form. People are trying to help you! – tooluser Aug 20 '14 at 18:14
2 Answers
Well, I can guarantee that the computer will have no problem with it. They're pretty good with large numbers.
Without knowing more about your situation, it's hard to say. It does sound, though, like you're potentially going about it in a way that isn't optimal. It's good that you followed your intuition and are asking about it. Perhaps you should modify your question (or ask a new one and link it here) to talk about what you really want to accomplish, and then we can help.

- 1,481
- 15
- 21
-
Well I stopped writing the code and asked the question here, but basically what I'm trying to accomplish is having a general helper class that contains static methods such as to load sounds, load images, etc... and static members to hold variables like if we need to mute the sound or not, or path to image to load – hakuna matata Aug 11 '14 at 18:00
-
Instead of variables use class methods in your helper class. That also allows you to make substantial changes to them later without upsetting other existing code. They could later be made tone dynamic if necessary with no changes to the using code. – zaph Aug 11 '14 at 18:07
-
Without more information that's a tough call. Technically working - yes.
I know many people don't like singletons, but maybe this is one of the good use cases for it?
Maybe you find that configuring one of those classes, or now objects, really doesn't have to be a singleton at all?
Just because there is only one instance of a given class doesn't mean in cannot be a "normal" class.
Class variables often mystify your state all over your code base and make debugging and code reuse a pain. Let alone multi threading.
Edit: Given your usecase as described in a comment to another answer, I'd go with a singleton, i.e. a 'SoundPlayer' class. '[[SoundPlayer sharedInstance] playCoolSound];' is easy, and you get proper instance variables there, too. And you can always exchange it for another class if needed (think test cases etc.).

- 25,601
- 15
- 56
- 71