In Objective-c how can we have a variable whose scope is the whole class (but doesn't include subclasses)

[亡魂溺海] 提交于 2019-12-20 05:50:25

问题


I created tons of subclasses of UITableViewCell. Each of which is a subclass of an additional BGBaseTableViewCell class.

When asked for height, I think it'll be useful to tell users what's the typical size of the UITableViewCell.

One way to do it is to specify that twice.

One when I design the UITableViewCell in xib, and the second time when I hard coded the typical size.

This is a design flaw because then I will need to remember to change the hard coded typical size when I change the stuff in xib.

I don't want that. I want to design my UITableViewCell in xib and when the class is initialized I want to store the default height.

the default height need to be different for all subclasses. However, it need to be the same for all instances for that subclass.

If I use simple ivar, then I will have to store that one default size for all instance.

If I use static variable, then the static variable in BGBaseTableViewCell is used by all it's subclasses.

So what should I do?

This is what I am doing now:

static CGFloat defaultHeightSet = 0; //this one is used by all subclasses. I want each subclass have it's own defaultHeightSet, but I want to specify that fact once.

+(void)initialize
{
    BGBaseTableViewCell * typical = [[self alloc]init];
    defaultHeightSet = typical.bounds.size.height;
}
+(CGFloat) defaultHeight
{
    return defaultHeightSet;
}

回答1:


Visible in whole class but not for subclasses? You can try this code for your .m file:

@interface ClassYouNeed ()
static VariableClass* variableVisibleInClassButNotInSubclass;
@end

Just make a "hidden" category on your class near your implementation, it should work.




回答2:


Why didn't I think of it.

Make the static variable a dictionary and set the class names as the keys :)

Ladies and gentlemen, children of all ages. Sharen Eayrs production proudly presents, statically protected variables:

static NSMutableDictionary * defaultHeightDictionary= nil;
static NSMutableDictionary * defaultBoundsDictionary =nil;
//static CGFloat defaultHeightSet = 0;

+(void)initialize
{
    BGBaseTableViewCell * typical = [[self alloc]init];

    if (defaultHeightDictionary==nil) {
        defaultHeightDictionary = [NSMutableDictionary dictionary];
        defaultBoundsDictionary = [NSMutableDictionary dictionary];
    }
    [defaultHeightDictionary setValue:@(typical.bounds.size.height) forKey:NSStringFromClass([self class])];
    CGRect bounds = typical.bounds;

    NSValue * boundsValue = [NSValue valueWithCGRect:bounds];
    [defaultHeightDictionary setValue:boundsValue forKey:NSStringFromClass([self class])];


}
+(CGFloat) defaultHeight
{
    NSNumber * result = [defaultHeightDictionary valueForKey:NSStringFromClass([self class])];
    return result.floatValue;
}

+(CGRect) defaultBounds
{
    NSValue * result = [defaultBoundsDictionary valueForKey:NSStringFromClass([self class])];
    return [result CGRectValue];
}


来源:https://stackoverflow.com/questions/13738109/in-objective-c-how-can-we-have-a-variable-whose-scope-is-the-whole-class-but-do

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!