Subclassing NSManagedObject with swift 3 and Xcode 8 beta

ぐ巨炮叔叔 提交于 2019-12-17 18:18:21

问题


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


回答1:


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.




回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/37905456/subclassing-nsmanagedobject-with-swift-3-and-xcode-8-beta

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!