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

好久不见. 提交于 2019-12-02 07:38:13

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.

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