在Objective-c中,我们如何拥有一个范围为整个类的变量(但不包括子类) [英] In Objective-c how can we have a variable whose scope is the whole class (but doesn't include subclasses)

查看:86
本文介绍了在Objective-c中,我们如何拥有一个范围为整个类的变量(但不包括子类)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了大量的UITableViewCell子类.每个都是额外的BGBaseTableViewCell类的子类.

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

当询问高度时,我认为告诉用户UITableViewCell的典型大小会很有用.

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.

一次是在xib中设计UITableViewCell时,第二次是对典型大小进行硬编码.

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

这是一个设计缺陷,因为当我更改xib中的内容时,我将需要记住更改硬编码的典型大小.

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.

我不要那个.我想在xib中设计UITableViewCell,并在初始化类时存储默认高度.

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.

如果我使用简单的ivar,则必须为所有实例存储一个默认大小.

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

如果我使用静态变量,那么BGBaseTableViewCell中的静态变量将由其所有子类使用.

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

那我该怎么办?

这就是我现在正在做的事情:

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;
}

推荐答案

为什么我没有想到它.

使静态变量成为字典,并将类名设置为键:)

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

女士们,先生们,各个年龄段的孩子. Sharen Eayrs生产自豪地提出了静态保护的变量:

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];
}

这篇关于在Objective-c中,我们如何拥有一个范围为整个类的变量(但不包括子类)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆