tailieunhanh - Lập trình Wrox Professional Xcode 3 cho Mac OS part 44

Tạo lớp con NSMANAGEDOBJECT Sự khởi đầu của chương này nói rằng các thực thể bạn defi ne trong mô hình dữ liệu của bạn tồn tại như trường hợp của NSManagedObject trong thời gian chạy. Khá thường xuyên, | 334 I CHAPTER 15 DATA MODELING 15. Add the following code to your application s data store setup NSDictionary options NSDictionary dictionaryWithObjectsAndKeys NSNumber numberWithBool YES NSMigratePersistentStoresAutomaticallyOption NSNumber numberWithBool YES NSInferMappingModelAutomaticallyOption nil 16. Pass the newly created options object in the -addPersistentStoreWithType configuration URL options error message during startup. Your application is now ready to run. When it starts Core Data detects that your current data model does not agree with the structure of your existing store. It automatically finds the older version of the data schema that does finds the mapping that maps between those two models applies the mapping and updates your data store. CREATING NSMANAGEDOBJECT SUBCLASSES The beginning of this chapter mentioned that the entities you define in your data model exist as instances of NSManagedObject at run time. Quite often NSManagedObject is more than sufficient for your needs. You might however need a class with more specialized functionality for many reasons Localized business logic in the entity object Specialized or complex validation Custom pre- or post-processing of attribute changes Non-standard attribute types There are also a number of reasons why you wouldn t need to subclass NSManagedObject Provide attribute property storage Use any of the built-in Core Data property features Inherit properties from other entities Use Key Value Coding Key Value Observing or Bindings An NSManagedObject reads a description of its entity and dynamically synthesizes real Objective-C accessor methods at run time. The result is an object that for all intents and purposes is identical to a custom class that defines those properties in code. As an example an NSManagedObject for an entity that defines a string name attribute property would be functionally identical to a custom class that implemented accessors compatible with a @property NSString name directive. .