0

I am trying to make Class methods (I do not want instance methods and variables) for a calculator class in Objective-C but I am running into issues. I have the following .h file:

#import <Foundation/Foundation.h>

@interface Calc : NSObject
{
NSNumber* accumulator;
}
//Acumulator Methods
+ (void) setAccumulator: (NSNumber *) value;
+ (void) clear;
+ (NSNumber *) accumulator;

//Arithmatic metods
+ (void) add: (NSNumber *) value;
+ (void) subtract: (NSNumber *) value;
+ (void) multiply: (NSNumber *) value;
+ (void) divide: (NSNumber *) value;

+ (void) setCurrentOperation: (NSString *) o;
+ (NSString *) currentOperation;

Then when I try to define any one of the methods in the .m file I receive the error: "instance variable 'x' accessed in class method". An example of a method in my .m file with this error being:

+ (void) setAccumulator: (NSNumber *) value
{
      accumulator = value;
}

Could someone perhaps explain why this error occurs as well as help me fix it? Thanks!

rmaddy
  • 314,917
  • 42
  • 532
  • 579
theGuy05
  • 417
  • 1
  • 7
  • 22
  • You're declaring `accumulator` as an instance variable, which means it can't be accessed from class methods. Instead, you should create it as a static variable in your .m file. See this question for info on how to do that: http://stackoverflow.com/questions/1063229/objective-c-static-class-level-variables – smileyborg May 05 '14 at 00:46
  • This is a poor design choice. Why are you making all of these methods class methods? They should all be instance methods. – rmaddy May 05 '14 at 00:50
  • 1
    I know you said that you don't want to use instance methods, but using instance methods/variables and then creating a (possibly global) Calc instance would be a better way of doing this. – Chris Devereux May 05 '14 at 00:50

3 Answers3

0

As the error says, you can't access instance variables in class methods, because instance variables belong to instances, not to classes. A class method is not linked to an instance, by definition, so there is no instance variable for it to change.

In general, class methods should have a reason for being there. For instance, a factory function for returning an instance of a particular class is a good candidate for a class method, because it's closely associated with that class. Otherwise, if you have a method which doesn't need to access any instance data, then it's generally better implemented as a standalone function. Objective-C is not like Java, where everything must be stuffed into a class, whether it belongs in one or not.

Crowman
  • 25,242
  • 5
  • 48
  • 56
0

If you don't want instance variables, then don't try to use them — in this case, accumulator is an instance variable that you're trying to use. If you do want instance variables, use instance methods.

Chuck
  • 234,037
  • 30
  • 302
  • 389
0

When I see code like this, it makes me think that you are really just abusing Object Oriented Programing...

you should be thinking about these objects as being distinct machines.. I assume you would want more than one calculator at some point... so you shouldn't be using class methods... your data should be contained in the instance of the calculator...

if you wanted to use class methods you would do things like changing the properties of all calculators, like you maybe wanted to change the default precision of fixed point calculations, or something like that, or to use one multiplication algorithm over another... but generally you would really just initialize the individual objects properties with those values and not have the class methods change the already initialized objects... this is much better for encapsulation.

Grady Player
  • 14,399
  • 2
  • 48
  • 76