iOS 中 flags 结构的用途是什么?
struct
{
unsigned resizesCellWidths:1;
unsigned numColumns:6;
unsigned separatorStyle:3;
unsigned allowsSelection:1;
unsigned backgroundViewExtendsUp:1;
unsigned backgroundViewExtendsDown:1;
unsigned usesPagedHorizontalScrolling:1;
unsigned updating:1;
unsigned ignoreTouchSelect:1;
unsigned needsReload:1;
unsigned allCellsNeedLayout:1;
unsigned isRotating:1;
unsigned clipsContentWidthToBounds:1;
unsigned isAnimatingUpdates:1;
unsigned requiresSelection:1;
unsigned contentSizeFillsBounds:1;
unsigned delegateWillDisplayCell:1;
unsigned delegateWillSelectItem:1;
unsigned delegateWillSelectItemMultiTouch:1;
unsigned delegateWillDeselectItem:1;
unsigned delegateDidSelectItem:1;
unsigned delegateDidSelectItemMultiTouch:1;
unsigned delegateDidDeselectItem:1;
unsigned delegateGestureRecognizerActivated:1;
unsigned delegateAdjustGridCellFrame:1;
unsigned delegateDidEndUpdateAnimation:1;
unsigned dataSourceGridCellSize:1;
unsigned int isEditing:1;
unsigned __RESERVED__:1;
} _flags;
- 这个结构体的目的是什么?
- 每行末尾的
:1
符号表示什么? - 当没有显式类型时,
unsigned
修饰符的含义是什么?
谢谢
struct
{
unsigned resizesCellWidths:1;
unsigned numColumns:6;
unsigned separatorStyle:3;
unsigned allowsSelection:1;
unsigned backgroundViewExtendsUp:1;
unsigned backgroundViewExtendsDown:1;
unsigned usesPagedHorizontalScrolling:1;
unsigned updating:1;
unsigned ignoreTouchSelect:1;
unsigned needsReload:1;
unsigned allCellsNeedLayout:1;
unsigned isRotating:1;
unsigned clipsContentWidthToBounds:1;
unsigned isAnimatingUpdates:1;
unsigned requiresSelection:1;
unsigned contentSizeFillsBounds:1;
unsigned delegateWillDisplayCell:1;
unsigned delegateWillSelectItem:1;
unsigned delegateWillSelectItemMultiTouch:1;
unsigned delegateWillDeselectItem:1;
unsigned delegateDidSelectItem:1;
unsigned delegateDidSelectItemMultiTouch:1;
unsigned delegateDidDeselectItem:1;
unsigned delegateGestureRecognizerActivated:1;
unsigned delegateAdjustGridCellFrame:1;
unsigned delegateDidEndUpdateAnimation:1;
unsigned dataSourceGridCellSize:1;
unsigned int isEditing:1;
unsigned __RESERVED__:1;
} _flags;
- What is the purpose of this struct?
- What does the
:1
notation at the end of each line signify? - What is the meaning of
unsigned
modifier when there is no explicit type?
Thanks
这些是位字段。由于其中大部分都是标志,因此它们只有 2 个可能的值,因此无需为每个字段分配超过 1 位。 (该结构中有几个例外)
unsigned
可以单独作为一种类型。它只是一个unsigned int
。Those are bitfields. Since most of these are flags, they only have 2 possible values, so there's no need to assign more than 1 bit to each field. (with a couple exceptions in that struct)
unsigned
can stand alone as a type. It's just anunsigned int
.