问题描述
我一直在尝试添加核心数据。
每次出现同样的错误:
错误:文件名EntityName + CoreDataClass.swift '/ Users / userName / Desktop / Development / MyApp / AppName / EntityName + CoreDataClass.swift'和'/Users/userName/Library/Developer/Xcode/DerivedData/AppName-dgwzrmxsetzvtedibxrazuutjwnh/Build/Intermediates/AppName.build/Debug-iphoneos /AppName.build/DerivedSources/CoreDataGenerated/Model/EntityName + CoreDataClass.swift'
我添加核心数据使用以下步骤:
1.新文件/ DataModel;保存在我的项目的根目录
选择Model.xcdatamodeld并添加实体,添加几个属性,保存,
编辑器/ create NSManagedObjectClass子类。
结果我在导航器中观察到4个新文件:
Model.xcdatamodeld,EntityName + CoreDataProperties.swift,EntityName + CoreDataClass.swift, _COREDATA_DATAMODELNAME _ + CoreDataModel.swift
其内容:
_COREDATA_DATAMODELNAME _ + CoreDataModel.swift:
import Foundation
import CoreData
___COREDATA_DATAMODEL_MANAGEDOBJECTCLASSES_IMPLEMENTATIONS___
EntityName + CoreDataClass.swift:
import Foundation
import CoreData
类EntityName:NSManagedObject {
}
EntityName + CoreDataProperties。 swift:
import Foundation
import CoreData
扩展EntityName {
@nonobjc class func fetchRequest() - > NSFetchRequest EntityName> {
return NSFetchRequest< EntityName>(entityName:EntityName);
}
@NSManaged var str:String?
}
我试过的:
1.清理构建,删除DerivedData,删除var /文件夹的内容,重新启动
2.删除生成的文件,显示在导航器中
我做错了什么?
Xcode 8包括自动 NSManagedObject
当模型文件使用Xcode 8文件格式时生成类。如果你创建自己的子类文件,你正在创建重复。错误消息中的第二个文件, DerivedSources
是Xcode自动创建的文件。
如果自动
如果你想创建自己的子类,你可以用
来创建你自己的子类, >- 将模型文件的工具版本设置为Xcode 7.3或更早版本以禁用所有代码生成(这似乎没有任何意义关于实际文件内容)或
- 通过将实体的Codegen设置设置为手动/无,为每个实体单独停用自动生成。
I have been trying to add core data.And every time I got the same error:
error: filename "EntityName +CoreDataClass.swift" used twice: '/Users/userName/Desktop/Development/MyApp/AppName/EntityName +CoreDataClass.swift' and '/Users/userName/Library/Developer/Xcode/DerivedData/AppName-dgwzrmxsetzvtedibxrazuutjwnh/Build/Intermediates/AppName.build/Debug-iphoneos/AppName.build/DerivedSources/CoreDataGenerated/Model/EntityName +CoreDataClass.swift'
I add core data using the following steps:
1.New file/ DataModel; save it in the root dir of my project
select Model.xcdatamodeld and add entity, add several attributes, save,editor/create NSManagedObjectClass Subclass.
As a result I observe 4 new files in navigator:Model.xcdatamodeld, EntityName+CoreDataProperties.swift, EntityName +CoreDataClass.swift, _COREDATA_DATAMODELNAME_+CoreDataModel.swift
their content:_COREDATA_DATAMODELNAME_+CoreDataModel.swift:
import Foundation
import CoreData
___COREDATA_DATAMODEL_MANAGEDOBJECTCLASSES_IMPLEMENTATIONS___
EntityName +CoreDataClass.swift:
import Foundation
import CoreData
class EntityName: NSManagedObject {
}
EntityName+CoreDataProperties.swift:
import Foundation
import CoreData
extension EntityName {
@nonobjc class func fetchRequest() -> NSFetchRequest< EntityName > {
return NSFetchRequest< EntityName >(entityName: "EntityName");
}
@NSManaged var str: String?
}
What I have tried:
1. Clean build, remove DerivedData, delete content of var/folders, restart
2. Delete generated files, displayed in navigator
All my efforts were out of luck.
What I am doing wrong?
Xcode 8 includes automatic NSManagedObject
class generation when the model file uses the Xcode 8 file format. If you create your own subclass files, you're creating duplicates. The second file in the error message, in DerivedSources
, is the one that Xcode created automatically.
If the automatically generated files do what you need, just stop creating your own and you'll be OK.
If you want to create your own subclasses instead, you can either
- Set the "tools version" for the model file to be Xcode 7.3 or earlier to disable all code generation (this doesn't seem to change anything meaningful about the actual file contents), or
- Disable automatic generation for each entity individually by setting the "Codegen" setting to "Manual/None" for the entity.
这篇关于Xcode-beta 8.无法创建核心数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!