Xcode 8 Core Data 生成代码 编译错误

错误描述

今天在Xcode 8下使用Core Data时,编译时出现以下错误:

<unknown>:0: error: filename "Friend+CoreDataClass.swift" used twice: '/Users/.../Documents/Demo/Friend/Friend/Friend+CoreDataClass.swift' and '/Users/.../Library/Developer/Xcode/DerivedData/Friend-adfqrgytnrhhxnavbipcabnyztke/Build/Intermediates/Friend.build/Debug-iphonesimulator/Friend.build/DerivedSources/CoreDataGenerated/Friend/Friend+CoreDataClass.swift'
<unknown>:0: note: filenames are used to distinguish private declarations with the same name
<unknown>:0: error: filename "Friend+CoreDataProperties.swift" used twice: '/Users/.../Documents/Demo/Friend/Friend/Friend+CoreDataProperties.swift' and '/Users/.../Library/Developer/Xcode/DerivedData/Friend-adfqrgytnrhhxnavbipcabnyztke/Build/Intermediates/Friend.build/Debug-iphonesimulator/Friend.build/DerivedSources/CoreDataGenerated/Friend/Friend+CoreDataProperties.swift'
<unknown>: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

仔细看下上面的报错,发现是文件名冲突。Friend+CoreDataClass.swift 和 Friend+CoreDataProperties.swift 同时出现在两个路径下:

/Users/.../Documents/Demo/Friend/Friend/
/Users/.../Library/Developer/Xcode/DerivedData/Friend-adfqrgytnrhhxnavbipcabnyztke/Build/Intermediates/Friend.build/Debug-iphonesimulator/Friend.build/DerivedSources/CoreDataGenerated/Friend/

产生原因

Xcode 8中实体新添了一个Codegen选项,而其默认值是'Class Definition',表示自动为实体生成对应类文件(上面较长路径下的文件)。

自动生成类文件.png

而我又用使用Editor->Create NSManagedObject Subclass手动为实体生成了类文件(上面较短路径下的文件)。所以导致了文件重名。

解决方案

方案一:删掉手动生成的类文件,直接使用自动生成的。
方案二:如果不想用系统自动生成的,我们可以将Codegen选项改为'Manual/None'。表示手动生成。

手动生成类文件.png

注意,使用方案二需要删除已经自动生成的文件(上面较长路径下的文件)。如果还是报错,使用Product -> Clean清理下试试。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,198评论 4 61
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,868评论 18 139
  • 在我看来,当下多数互联网营销的实质总结起来就是两句话—— 一群月薪一万的人教一群月薪五千的人如何过月薪五万的生活;...
    小召大叔阅读 161评论 0 0
  • 皆因情切坠凡尘,已近中年不惑身。 且羡困龙归赤海,又携倦客入寒村。 薄田五亩足活我,陋室三间可宴君。 后院紫茄霜色...
    文场倦客阅读 277评论 3 7
  • 几天以后。。。 请大家严肃~~刚才播放的片段来自最近热映的灾难大片《大裁员》,相信大家都已经看过了,什么?有的朋友...
    Boss直聘阅读 459评论 2 0