Creating Swift Models from Core Data Objects

Update for Xcode 8:

In Xcode 8, you need to go to the master data model editor and display “File Inspector”. Near the bottom there is an option for generating code. Choose Swift.

Change I found a solution to create a Swift model from a Core Data object:

In Xcode:

Editor> Create NSManagedOjbect> Click Next> Click Next> Select Swift Langage> Click Create




I tried Swift langage, creating a new Swift project for Xcode 6 beta using Core Data.

When I create my models from Core Data objects, Xcode creates Objective-C models.

Is there a way to generate a Swift model, not an Obejctive-C model with master data?

Thank!

+47
xcode swift core-data model nsmanagedobject
Jun 03 '14 at 12:28
source share
6 answers

Let's look at the Objective-C method:

Person.h (header file)

#import <Foundation/Foundation.h> #import <CoreData/CoreData.h> @interface Person : NSManagedObject @property (nonatomic, retain) NSString *name; @end 

Person.m (implementation file)

 #import "Person.h" @implementation Person @dynamic name; @end 

Swift

The documentation already included in Xcode6-Beta says:

Core Data provides basic storage and implementation of properties in subclasses of the NSManagedObject class. Add the @NSManaged attribute before each property definition in a subclass of the managed object that corresponds to the attribute or relationships in your kernel data model. Like the @dynamic attribute in Objective-C, the @NSManaged attribute informs the Swift compiler that the storage and implementation of the property will be provided at run time. However, unlike @dynamic, the @NSManaged attribute is only available to support Core Data.

Thus, I would rewrite the above example for Swift (not tested):

Person.swift

 import CoreData class Person: NSManagedObject { @NSManaged var name : NSString } 

And on your question, I think the subclass-generating function is not yet included in Xcode6. Did you make sure you selected "Swift" as the programming language when you created Cocoa -Project in Xcode?

+30
Jun 03 '14 at 12:55 on
source share

You can return the Swift model with NSEntityDescription.insertNewObjectForEntityForName , but you have to edit the master data model file and not use Person as a class object, but <ProjectName>.Person else it returns NSManagedObject ...

Using println() , you will not see an instance of Person , but something like <_TtC5ProjectName4Person: 0xc9ad5f0> , but method calls on it will serve as an example of Person for real. I think this is just a way for Swift to generate unique class names, not conflicts, and CoreData's methods show this internal mechanism.

Apple Documentation :

Swift classes are namespaced-theyre tied to the module (usually the project) in which they are compiled. To use the Swift subclass of NSManagedObject with the Core Data model, prefix the class name in the Class field in the object inspector model with the name of your module.

+12
Jun 14 '14 at 17:54 on
source share

According to Apple's video about what's new in the CoreData frame: 38 minutes (WWDC2014 session), the inspector data model has a class name prefix with the project name. Like projectName.Doctor

I tried this, but it happens that the created managed object class becomes: projectName.swift instead of Doctor.swift . Even the class declaration becomes class projectName: ManagedObject

Decision:

In the data model inspector, simply specify the name and class of your object, what name you want, example: Doctor

After creating the object model and selecting Swift, this will create a file ( Doctor.swift ).

Now, when inserting new records into Core Data, you may encounter the error of the experience “Class not found using NSManagedObject by default”, even if you added the newly inserted object to the correct object name.

To solve this problem, simply add @objc (class name) above the class declaration. See the example below.

 import Foundation import CoreData @objc(Doctor) class Doctor: NSManagedObject { @NSManaged var name: String } 

Then:

 let doctorManagedObject = NSEntityDescription.insertNewObjectForEntityForName("Doctor", inManagedObjectContext: context) as Doctor doctorManagedObject.name = "John" // you can now use dot syntax instead of setValue 

Save context to fix insert.

+11
Sep 22 '14 at 9:58
source share

I tested @NSManaged, this did not work. :(. But the mixed model (.h) files generated by xcdatamodel succeed. Please read the document and code at https://github.com/iascchen/SwiftCoreDataSimpleDemo

+3
Jun 07 '14 at 10:26
source share

Alternatively, you can simply add #import "Person.h" to the bridge header, Project-Bridging-Header.h , which Xcode generates for you (if you accepted its proposal for generation). You can then use all the auto-generated Obj-Cs as if they were native Swift.

+3
Jun 15 '14 at 23:34
source share

Editor -> Subclass NSManagedObject works fine for Swift

Just go through all the usual steps, but when you start creating files, select "Swift" if it is the first time you used Create NSManagedObject Subclass with a Swift project, Objective C will default

+2
Dec 17 '15 at 16:07
source share



All Articles