Skip to content

Instantly share code, notes, and snippets.

@macmade
Created December 10, 2011 17:28
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save macmade/1455673 to your computer and use it in GitHub Desktop.
Save macmade/1455673 to your computer and use it in GitHub Desktop.

Coding Guidelines for Cocoa

Tips and Techniques for Framework Developers

http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CodingGuidelines/Articles/FrameworkImpl.html#//apple_ref/doc/uid/20001286-BAJIBFHD

Object Sizes and Reserved Fields

Each Objective-C object has a size that can be determined by the total size of its own instance variables plus the instance variables of all superclasses. You cannot change the size of a class without requiring the recompilation of subclasses that also have instance variables. To maintain binary compatibility, you usually cannot change object sizes by introducing new instance variables into your classes or getting rid of unneeded ones.

So, for new classes, it's a good idea to leave a few extra “reserved” fields for future expansion. If there are going to be few instances of a class this is clearly not an issue. But for classes instantiated by the thousands, you might want to keep the reserved variable small (say, four bytes for an arbitrary object).

For older classes whose objects have run out of room (and assuming the instance variables were not exported as public), you can move instance variables around, or pack them together in smaller fields. This rearranging may allow you to add new data without growing the total object size. Or you can treat one of the remaining reserved slots as a pointer to an additional block of memory, which the object allocates as it is initialized (and deallocates as it is released). Or you can put the extra data into an external hash table (such as a NSDictionary); this strategy works well for instance variables that are seldom created or used.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment