Objective-C Class Initialization
A template for shared resources
Class variables are incredibly useful for constants and other expensive objects. Getting them initialized with objects can be a little more complicated.
Static variables intended to be used by a class need to be accessable by that class. To allow for this I like to define them above
@implementation and after the
@interface extension. To follow convention they should also be prefixed with a lowercase
Examples will be based around a date formatter. This is commonly done as
NSDateFormatter is expensive to produce.
The +initialize Method
+initialize method is called before any instances of a class are created. While they shouldn’t be called more then once per class they are. If a subclass fails to create its own
+initialize method its parents is called. Traditionally a check is preformed for the class type to guard against this.
While this guards against the previously mentioned issue its not foolproof. Suppose somebody calls
+initialize by mistake. This would pass the check and a second instance would be created without release the previous instance. This can theoretically be run more then once if its called on the same class which could cause leaks and perform unnecessary work. The modern approach is to use
dispatch_once() which will only ever be called once. As the token doesn’t need to be accessable outside of the method its placed in the
Putting It All Together
Any source in this article is released under the ISC License.