Iphone 存储iOS UI组件内部使用标志
大多数苹果的实现,我可以看到他们使用一个结构来存储标志位,他们为什么这样做?。为什么我们不能用BOOL来处理呢 请参见下面tableview中的苹果示例代码Iphone 存储iOS UI组件内部使用标志,iphone,ios,objective-c,ipad,Iphone,Ios,Objective C,Ipad,大多数苹果的实现,我可以看到他们使用一个结构来存储标志位,他们为什么这样做?。为什么我们不能用BOOL来处理呢 请参见下面tableview中的苹果示例代码 struct { unsigned int delegateheightForHeaderInSection:1; unsigned int dataSourceCellForRow:1; unsigned int delegateHeightForRow:1;
struct {
unsigned int delegateheightForHeaderInSection:1;
unsigned int dataSourceCellForRow:1;
unsigned int delegateHeightForRow:1;
unsigned int style:1;
} _tableFlags;
在内部,他们可能会使用类似的东西
_tableFlags.delegateheightForHeaderInSection = [delegate respondsToSelector:@selector(tableView:heightForHeaderInSection:)];
到处使用“\u tableFlags.delegateheightForHeaderInSection”检查用户是否实现了此委托方法
因此,我们不能像下面那样实现,而不是使用结构来存储标志
BOOL delegateheightForHeaderInSection;
这样用,
delegateheightForHeaderInSection = [delegate respondsToSelector:@selector(tableView:heightForHeaderInSection:)];
这两种方法有什么区别
unsigned int delegateheightForHeaderInSection:1;
在结构中定义长度为1的位字段(参见示例)。
位字段可用于节省空间。因此在本例中,四个成员delegateheightForHeaderInSection
,…,style
存储在一个整数的连续位中
请注意,在这种特殊情况下不会节省空间。\u tableFlags
的大小是unsigned int
的大小,即4。四个BOOL
(又称unsigned char
)成员的大小也是4
但是,例如,长度为1的32位字段也需要4个字节,而32
BOOL
成员需要32个字节。我认为从技术上讲,这没有什么大的区别,只是为了方便而包装内容。