Letztens haben wir core data zu einem Swift Projekt dazu getan, indem wir einfach mal ein Core Data Modell per Drag-and-Drop dazu packen. Leider übersetzt Xcode das so dazu gefügte Modell nicht korrekt. Ein neu erzeugtes Modell in das wir die Entity-Definitionen des alten Modells hineinkopiert hatten, funktionierte dann aber problemlos. Merke: Beim neu anlegen von Core Data Modellen wird mehr gemacht, als nur eine Datei erzeugt.
Als dann der Zugriff auf die per Editor->Create NSManagedObject Subclass erzeugte Klasse nicht funktionierte, erinnerte ich mich dann glücklicherweise nach ein paar Minuten, dass ich ja in dem iBook „Using Swift with Cocoa and Objective-C“ gelesen hatte, dass man den Projektnamen in die Klassendefinition dazu schreiben muss, weil Swift ja kein flaches Klassenmodell hat, sondern bei einer Klasse den Namen des Projektes voranstellt.
Kaum hatten wir das gemacht, funktionierte alles. Ich frage mich nur, ob die Entity jetzt auch noch mit Objective-C funktionieren würde. Aber das probiere ich ein anderes mal.When we decided to add core data to a swift project, we thought, a simple drag and drop of the core data model file would do the job. After some time wondering why it did not work, we finally found out, that xcode did not compile the file correctly, creating only a mom-file and not the momd-file package. The fix was easy, of course. We only had to create a new core data model file using New->File->… and then to copy the content from the old to the new model, which then worked as expected.
The other surprise of that day came while trying to access the class that we had created using Editor->Create NSManagedObject Subclass. We could not correctly use the subclass as we had done it using Objective-C. Again, after some minutes I remembered, what I had read in the iBook „Using Swift with Cocoa and Objective-C“, that since Swift has no flat class model, we needed to include the class name into the Class-Field of our Entity.
We did that by selecting the entity, opened the model entity inspector and prefixed our entity class with the project name separated by a dot. I just wonder if that class name ( e.g. project.entity ) would still be accessible using Objective-C as well. May be I’ll try that next time.