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

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

Like this article? We recommend

Default Values

There are two different ways to handle default values for newly created Core Data objects. Which route to take depends on the type of data. If the default value is something static, such as "New Value" in a string, that can be just set in the model itself. But what if the current date needs to be set as a default value? One way to handle it is to set the create date every time a new object is instantiated, but that risks difficulties and potential errors during future updates. Another way to handle this situation is to extend NSManagedObject and utilize the awakeFromInsert: method. This method is called automatically by the Framework when a new instance of the entity is created. Therefore, in the example entity above, the following method would guarantee that the create date is set on every new instance:

- (void)awakeFromInsert {
 [self setValue:[NSDate date] forKey:@"createDate"];
}
  • + Share This
  • 🔖 Save To Your Account