Search code examples
objective-cstaticclass-design

Is this a good way to have class level member variables?


I'm trying to violate the laws of objective C a little by having static (class level) variables that have setters and getters:

+(CGRect*)defaultOpFr:(CGRect*)set{
    static CGRect * defaultOpFr = nil;

    if (set) {
        if (!defaultOpFr) {
            defaultOpFr = malloc(sizeof(defaultOpFr));
        }
        defaultOpFr->size.width = set->size.width;
        defaultOpFr->size.height = set->size.height;
        defaultOpFr->origin.x = set->origin.x;
        defaultOpFr->origin.y = set->origin.y;
    }
    return defaultOpFr;
}

It seems to work, but I'm wondering if there's a better way. The idea is to call it with nil to retrieve the value, call it with a CGRect to set a new value.


Solution

  • Yup; that'll work, but be completely against any kind of common pattern.

    Why don't you simply have a standard setter/getter pair? Even at the class level, that is fine:

    static CGRect barf;
    + (CGRect) barf { return barf; }
    + (void) setBarf:(CGRect)aRect { barf = aRect; }
    

    Done.