xcode “文件名用于区分同名的私有声明”错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40366329/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 09:32:02  来源:igfitidea点击:

'filenames are used to distinguish private declarations of the same name' error

iosxcodecore-datansmanagedobject

提问by Swasidhant

I am getting this error on generating an NSManagedObject in Xcode 8.1 in Swift.

我在 Swift 的 Xcode 8.1 中生成 NSManagedObject 时遇到此错误。

:0: error: filename "DemoOne+CoreDataClass.swift" used twice: '/Users/Swasidhant/Desktop/demo again/DemoOne+CoreDataClass.swift' and '/Users/Swasidhant/Library/Developer/Xcode/DerivedData/demo_again-hiinrbwwbmyfbrbctsfdzvudkkuy/Build/Intermediates/demo again.build/Debug-iphonesimulator/demo again.build/DerivedSources/CoreDataGenerated/Model/DemoOne+CoreDataClass.swift' :0: note: filenames are used to distinguish private declarations with the same name :0: error: filename "DemoOne+CoreDataProperties.swift" used twice: '/Users/Swasidhant/Desktop/demo again/DemoOne+CoreDataProperties.swift' and '/Users/Swasidhant/Library/Developer/Xcode/DerivedData/demo_again-hiinrbwwbmyfbrbctsfdzvudkkuy/Build/Intermediates/demo again.build/Debug-iphonesimulator/demo again.build/DerivedSources/CoreDataGenerated/Model/DemoOne+CoreDataProperties.swift' :0: note: filenames are used to distinguish private declarations with the same name Command /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swiftc failed with exit code 1

:0:错误:文件名“DemoOne+CoreDataClass.swift”使用了两次:'/Users/Swasidhant/Desktop/demo again/DemoOne+CoreDataClass.swift'和'/Users/Swasidhant/Library/Developer/Xcode/DerivedData/demo_again- hiinrbwwbmyfbrbctsfdzvudkkuy/Build/Intermediates/demo again.build/Debug-iphonesimulator/demo again.build/DerivedSources/CoreDataGenerated/Model/DemoOne+CoreDataClass.swift':0:注意:文件名用于区分同名的私有声明:0 :错误:文件名“DemoOne+CoreDataProperties.swift”使用了两次:'/Users/Swasidhant/Desktop/demo again/DemoOne+CoreDataProperties.swift'和'/Users/Swasidhant/Library/Developer/Xcode/DerivedData/demo_again-hiinrbwwbmyfbrbctsfyd再次构建/中间体/演示。再次构建/调试-iphonesimulator/演示。build/DerivedSources/CoreDataGenerated/Model/DemoOne+CoreDataProperties.swift' :0: 注意:文件名用于区分同名的私有声明 Command /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/ bin/swiftc 失败,退出代码为 1

I searched for this and got answers saying the file might be present twice in the project which is not my case. Another solution I came across was to remove the file from the build phase and add again. That too didn't work. Is it some build setting configuration mistake ? Or something else?

我搜索了这个并得到答案说该文件可能在项目中出现两次,这不是我的情况。我遇到的另一个解决方案是从构建阶段删除文件并再次添加。那也不起作用。是一些构建设置配置错误吗?或者是其他东西?

Here is the link with a demo project created with Xcode 8.1 and showing this problem:- https://www.dropbox.com/s/xwyzhshktb2hqe7/demo2.zip?dl=0

这是使用 Xcode 8.1 创建并显示此问题的演示项目的链接:- https://www.dropbox.com/s/xwyzhshktb2hqe7/demo2.zip?dl=0

回答by Midhun MP

The issue is happening because of the Xcode automatic subclass generationfeature.

由于Xcode 自动子类生成功能,此问题正在发生。

According to 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/Noneis the default, and previous behavior; in this case you should implement your own subclass or use NSManagedObject.
  • Category/Extensiongenerates 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 Definitiongenerates 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.

Xcode 自动子类生成

Xcode 现在支持在建模工具中自动生成 NSManagedObject 子类。在实体检查器中:

  • Manual/None是默认值和以前的行为;在这种情况下,您应该实现自己的子类或使用 NSManagedObject。
  • 类别/扩展在名为 ClassName+CoreDataGeneratedProperties 的文件中生成类扩展。您需要声明/实现主类(如果在 Obj-C 中,扩展可以通过头文件导入名为 ClassName.h)。
  • Class Definition生成名为 ClassName+CoreDataClass 的子类文件以及为 Category/Extension 生成的文件。

生成的文件放置在 DerivedData 中,并在保存模型后在第一次构建时重新构建。它们也由 Xcode 索引,因此命令单击引用并按文件名快速打开工作。


So for fixing your issue, you need to set the Codegenfield value to Manual/Noneas shown below: Core Data Codegen


因此,为了解决您的问题,您需要将Codegen字段值设置Manual/None为如下所示: 核心数据代码生成器

回答by superm0

This is what I did to fix this issue,

这就是我为解决此问题所做的工作

  1. Select your entity in the data model
  2. Set the Moduleto Current Product Module
  3. Leave the Codegento Class Definitionthough Manual/Nonealso works
  4. Select Editor > Create NSManagedObject Subclass
  1. 在数据模型中选择您的实体
  2. 模块设置为当前产品模块
  3. 保留代码生成类定义虽然手动/无也适用
  4. 选择编辑器 > 创建 NSManagedObject 子类

回答by Naveen Shan

May help someone, Go to project compile source delete the extra reference if any.

可能对某人有帮助,如果有的话,去项目编译源删除额外的参考。

回答by Yogendra Singh

Select code data model then select the Entity change the value of 'Codegen' to Manual/None.

选择代码数据模型,然后选择实体,将“Codegen”的值更改为手动/无。

It will work.

它会起作用。

回答by Darkwonder

The issue was due a second file named:"Extensions.swift" in my project.

问题是由于我的项目中的第二个文件名为:“Extensions.swift”。

After deleting the duplicate "Extensions.swift" the error got resolved. Your error message might tell you which file is duplicated.

删除重复的“Extensions.swift”后,错误得到解决。您的错误消息可能会告诉您哪个文件被复制。

回答by Ramis

In my case Data Model (as example: MyCoreData.xcdatamodeld) was listed twice under Editor -> Create NSManagedObject Subclass...And here was reason why it was getting this type of error.

在我的情况下,数据模型(例如:MyCoreData.xcdatamodeld)在Editor -> Create NSManagedObject Subclass下列出了两次......这就是为什么会出现此类错误的原因。

What I did:

我做了什么:

  1. I did delete MyCoreData.xcdatamodeld file.
  2. Created another Data Model as example MyCD.xcdatamodeld.
  3. Checked is here duplicated Data Models in here: Editor -> Create NSManagedObject Subclass...
  4. Clean and later build for two times.
  1. 我确实删除了 MyCoreData.xcdatamodeld 文件。
  2. 创建另一个数据模型作为示例 MyCD.xcdatamodeld。
  3. 在这里检查了重复的数据模型:编辑器-> 创建 NSManagedObject 子类...
  4. 清洁后构建两次。

回答by catanore

It happened to me when I copied and renamed entity. It failed to rename the class, so do it manually.

当我复制并重命名实体时,它发生在我身上。重命名类失败,所以手动重命名。

回答by Firda Sahidi

I just restart my Macbook and that clean my build folder. And it works like a charm!

我只是重新启动我的 Macbook 并清理我的构建文件夹。它就像一个魅力!