Home > Articles > Home & Office Computing > Mac OS X

  • Print
  • + Share This
Like this article? We recommend

Subclassing Still Exists

Even if delegates give the developer a powerful new option for Cocoa development, subclasses still exist. The most basic example of this is NSObject. Because every object extends from NSObject, it is required to at least extend from NSObject.

In the new core data framework, it is very common to subclass NSManagedObject as well. Although NSManagedObject handles all the heavy lifting for core data quite well, there are still certain things best handled by subclassing. For instance, I prefer to have a creation date set in every object I store in my applications. (A very old habit of mine brought on by dealing with a lot of DBAs.) Therefore, my core data objects routinely override the awakeFromInsert method as follows:

- (void)awakeFromInsert {
 [self setValue:[[[NSDate alloc] init] autorelease] forKey:@"createDate"];

With this simple method, I am assured that every new object I create in my core data applications will automatically have the create date set for them. This same method can be utilized to confirm that relationships are in place, set transient values, send out notifications, and so on.

  • + Share This
  • 🔖 Save To Your Account