Initializes a managed object from an entity description and inserts it into the specified managed object context.
- iOS 3.0+
- macOS 10.4+
- tvOS 9.0+
- watchOS 2.0+
- Core Data
The entity of which to create an instance.
The model associated with
context's persistent store coordinator must contain
The context into which the new instance is inserted.
An initialized instance of the appropriate class for
NSManaged uses dynamic class generation to support the Objective-C 2 properties feature (see Declared Properties) by automatically creating a subclass of the class appropriate for
init therefore returns an instance of the appropriate class for
entity. The dynamically-generated subclass will be based on the class specified by the entity, so specifying a custom class in your model will supersede the class passed to
context is not
nil, this method invokes
[context insert (which causes
awake to be invoked).
You are discouraged from overriding this method—you should instead override
awake (if there is logic common to these methods, it should be factored into a third method which is invoked from both). If you do perform custom initialization in this method, you may cause problems with undo and redo operations.
In many applications, there is no need to subsequently assign a newly-created managed object to a particular store—see
assign(_:. If your application has multiple stores and you do need to assign an object to a specific store, you should not do so in a managed object's initializer method. Such an assignment is controller- not model-level logic.
If you override
init(entity:, you must ensure that you set
self to the return value from invocation of
super’s implementation, as shown in the following example: