My question is about style/structure in Apple's Objective-C headers. Take NSSplitViewItem, for example:
NSSplitViewItem (Apple):
@interface NSSplitViewItem : NSObject {
@private
id _splitViewItemPrivateData;
struct {
unsigned int _collapsed:1;
unsigned int _canCollapseFromDrag:1;
unsigned int _canCollapseFromDoubleClickOnDivider:1;
unsigned int _reserved:29;
} _flags;
}
Why the Struct?
Why does Apple seem to favor using a struct of int flags for options? This pattern is incredibly common throughout their headers, even in places where it seems unnecessary—such as here. Why not just put those ints as iVars on the class rather than wrap them into a struct?
Is there some performance gain or other benefit? Here, the "flags" are all related to "collapasing". But in other classes, such as NSTableView, there are "flags" structs that have more than two dozen unrelated entries.
So is this just an Apple style, or is there something more meaningful?
bitfields(memory packing), which is generally more efficient — so in the example above the first threeintsused asboolonly take up3bits of storage. - l'L'l