0

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?

2
  • 2
    Why 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. Commented Aug 11, 2014 at 18:07
  • Asking questions without accepting answers: bad form. People are trying to help you! Commented Aug 20, 2014 at 18:14

2 Answers 2

1

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.).

Sign up to request clarification or add additional context in comments.

Comments

1

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.

3 Comments

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
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.
The usual solution here is to use a singleton object.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.