0

I've using the Objective-C singleton from here at stackoverflow.

The singleton in the class method accesses it's instance variable, which works, but throws a complie warning. How should I be doing this? Is there a way to do this without accessing the sharedInstance: in each class method?

for example here is my class method:

+ (NSString *)myClassMethods {
    [instanceDateFormatter setFormat:@"MM"];
    return [instanceDateFormatter stringWithDate:somedate];
}

line 2 will have the complie warning.

Thanks, Ross

Community
  • 1
  • 1
Ross
  • 14,266
  • 12
  • 60
  • 91

2 Answers2

3

You should be using the sharedInstance: call in each class method. I guess if you really want to, you could work around it with global variables, but the right solution is as you mentioned.

Carl Norum
  • 219,201
  • 40
  • 422
  • 469
2

Since instanceDataFormatter is an instance variable, you have to access it via a class instance -- so you'll need to go through your sharedInstance method to get it. Or, you could access it via the static singleton variable, bypassing the call to sharedInstance (however, that could break if the static variable hasn't been initialized yet).

mipadi
  • 398,885
  • 90
  • 523
  • 479