25

I'm declaring a family of static classes that deals with a communications protocol. I want to declare a parent class that process common messages like ACKs, inline errors...

I need to have a static var that mantain the current element being processed and I want to declare it in the parent class.

I do it like this:

parent.m

@implementation ServerParser

static NSString * currentElement;

but the subclasses are not seing the currentElement.

Cœur
  • 37,241
  • 25
  • 195
  • 267
gonso
  • 2,065
  • 5
  • 27
  • 35
  • 1
    Why are you using static variables and class methods? What if, in the future, you want to use your classes concurrently? What if you you need to handle more than 1 connection or otherwise need multiple instances? To be blunt, it looks like a broken design. – outis May 10 '09 at 09:12
  • 23
    That is a ridiculous statement. There are many uses for static variables. – IanStallings Jun 28 '11 at 17:24

3 Answers3

37

If you declare a static variable in the implementation file of a class, then that variable is only visible to that class.

You could declare the static variable in the header file of the class, however, it will be visible to all classes that #import the header.

One workaround would be to declare the static variable in the parent class, as you have described, but also create a class method to access the variable:

@implementation ServerParser

static NSString *currentElement;
...
+ (NSString*)currentElement
{
    return currentElement;
}
...
@end

Then, you can retrieve the value of the static variable by calling:

[ServerParser currentElement];

Yet the variable won't be visible to other classes unless they use that method.

Ramy Al Zuhouri
  • 21,580
  • 26
  • 105
  • 187
Alex Rozanski
  • 37,815
  • 10
  • 68
  • 69
  • Thanks for the tip. I didn't think of the access method. Anyway, I ended changing the "static" class to an "instance" behaviour. – gonso May 10 '09 at 23:13
  • 7
    WOAWOAOWAWOA: if you declare a static variable in the header, and then import the header, each file (compilation unit, actually) importing the header will be referring to a DIFFERENT variable of the same name. So if you say (for instance) currentElement = [[element alloc] init]; in the super class initialization, and then try to get currentElement in a subclass, currentElement will still be nil to the subclass! – Jared Pochtar Jun 16 '10 at 04:11
  • 1
    @Jared P I know it's been a while since this post, but can you explain how to _avoid_ the problem you're describing? – AWrightIV Jun 10 '11 at 21:03
  • @AWrightIV, declare the variable extern in the header. But the best approach of all is to only access the static variable via wrapper methods in the class. – Bill Aug 15 '11 at 01:07
  • Is the class method public or private? – user4951 Jun 03 '12 at 06:21
  • @JimThio Yeah, I know that answer took a while, but I believe a class method is always 'public'. It's the responsibility of the programmer to make sure it isn't used, where it's 'not supposed to be used'. If you want a private method, don't put it in the header. – ATaylor Jul 30 '12 at 08:26
5

A workaround would be to declare the static variable in the implementation of the parent class AND also declare a property in the parent class. Then in the accessor methods access the static variable. This way you can access static variables like properties with dot syntax. All the subclasses access the same shared static variable.

Морт
  • 1,163
  • 9
  • 18
-2

More simple. Create a pre Base class, with protected static variable. For example:

public abstract class preBase {

protected static int VariableStaticPrivate;

}

public abstract class Base : preBase{

//Inherit VariableStaticPrivate
//And you can use it.

}

public class DerivedOne : Base {

//Also inherit VariableStaticPrivate
//And you can use it.

}

  • 4
    lol may be one day you will know whats happening around you, who knows, one day! P.S. I didn't even down voted xD – parveen Feb 28 '15 at 15:19