Subclassing NSManagedObject with swift 3 and Xcode

2020-02-03 05:53发布

I've began to try use Core data with swift 3 and Xcode 8 beta. When I try to generate NSManagedObject subclasses from core data model and Create NSManagedObject subclass… option in Editor menu, Xcode 8 beta generates three files one of them is _COREDATA_DATAMODELNAME_+CoreDataModel.swift with the following content:

import Foundation
import CoreData

___COREDATA_DATAMODEL_MANAGEDOBJECTCLASSES_IMPLEMENTATIONS___

In addition, the content of this file shows two warnings:

Expressions are not allowed at the top level.
Use of unresolved identifier '___COREDATA_DATAMODEL_MANAGEDOBJECTCLASSES_IMPLEMENTATIONS___'

Has anyones faced the same issue? Which is the meaning of this new file?

Thanks

3条回答
放荡不羁爱自由
2楼-- · 2020-02-03 06:30

I've run into the problem with the .swift file cited above and found that commenting out the ___COREDATA... line got me by the error for now. I'm not sure what it's supposed to accomplish.

I'm also finding that XCODE 8 has a tendency to forget about new fields that are added to CoreData entities and to repeatedly reset the CoreData model code generation target to Objective C, leading to a flurry of .h and .m files instead of swift files when it regenerates files. Most of the issues that I've seen have been CoreData related, but have been workable so far.

Other than that, XCODE 8 has been surprisingly solid working with an app with 25 data entities and over 30 view controllers so I'm not complaining.

查看更多
【Aperson】
3楼-- · 2020-02-03 06:31

It's probably a (beta) clash with the new automatic subclass generation, which can be controlled in the entity inspector of the data model file.

From the documentation (What's New In Core Data)

Xcode automatic subclass generation

Xcode now supports automatic generation of NSManagedObject subclasses in the modeling tool. In the entity inspector:

  • Manual/None is the default, and previous behavior; in this case you should implement your own subclass or use NSManagedObject.
  • Category/Extension generates a class extension in a file named like ClassName+CoreDataGeneratedProperties. You need to declare/implement the main class (if in Obj-C, via a header the extension can import named ClassName.h). -
  • Class Definition generates subclass files named like ClassName+CoreDataClass as well as the files generated for Category/Extension.

The generated files are placed in DerivedData and rebuilt on the first build after the model is saved. They are also indexed by Xcode, so command-clicking on references and fast-opening by filename works.

查看更多
放我归山
4楼-- · 2020-02-03 06:32

I have similar problems with this developer beta Xcode 8. Some of them resolved Command + S (save changes) before I leave Data Model or before I generate NSManagedObject subclasses. I don't know why but in my case automatic save didn't work sometimes and some errors occur.

查看更多
登录 后发表回答