Xcode 8 为 iOS 10 生成损坏的 NSManagedObject 子类 [英] Xcode 8 generates broken NSManagedObject subclasses for iOS 10
问题描述
我最近将我的 iOS 应用程序项目更新到了 iOS 10.现在我正在尝试更改我的应用程序的核心数据模型,但 Xcode 生成的新 NSManagedObject 子类已损坏.我也尝试修复子类手册,但这不起作用.
I updated my iOS app project recently to iOS 10. Now I'm trying to change the Core Data Model of my app but the new NSManagedObject subclasses which Xcode generates are broken. I also tried to fix the subclasses manual but this doesn't work.
Core Data Model 的最低工具版本设置为 Xcode 7.0,代码生成语言设置为 Swift.
The minimum tools version for the Core Data Model is set to Xcode 7.0 and code generation language is set to Swift.
这是Xcode生成的代码:
This is the code which Xcode generates:
import Foundation
import CoreData
import
extension Group {
@nonobjc public class func fetchRequest() -> NSFetchRequest {
return NSFetchRequest(entityName: "Group");
}
@NSManaged public var name: String?
@NSManaged public var platform: NSNumber?
@NSManaged public var profiles: NSOrderedSet?
}
// MARK: Generated accessors for profiles
extension Group {
@objc(insertObject:inProfilesAtIndex:)
@NSManaged public func insertIntoProfiles(_ value: SavedProfile, at idx: Int)
@objc(removeObjectFromProfilesAtIndex:)
@NSManaged public func removeFromProfiles(at idx: Int)
@objc(insertProfiles:atIndexes:)
@NSManaged public func insertIntoProfiles(_ values: [SavedProfile], at indexes: NSIndexSet)
@objc(removeProfilesAtIndexes:)
@NSManaged public func removeFromProfiles(at indexes: NSIndexSet)
@objc(replaceObjectInProfilesAtIndex:withObject:)
@NSManaged public func replaceProfiles(at idx: Int, with value: SavedProfile)
@objc(replaceProfilesAtIndexes:withProfiles:)
@NSManaged public func replaceProfiles(at indexes: NSIndexSet, with values: [SavedProfile])
@objc(addProfilesObject:)
@NSManaged public func addToProfiles(_ value: SavedProfile)
@objc(removeProfilesObject:)
@NSManaged public func removeFromProfiles(_ value: SavedProfile)
@objc(addProfiles:)
@NSManaged public func addToProfiles(_ values: NSOrderedSet)
@objc(removeProfiles:)
@NSManaged public func removeFromProfiles(_ values: NSOrderedSet)
}
这些是 Xcode 给出的具体错误:
These are the specific errors which Xcode gives:
1. Group+CoreDataProperties.swift:13:1: Expected identifier in import declaration (the empty import)
2. Group+CoreDataProperties.swift:13:11: 'Group' is ambiguous for type lookup in this context
3. Group+CoreDataProperties.swift:15:16: Cannot specialize non-generic type 'NSFetchRequest'
4. Group+CoreDataProperties.swift:26:11: 'Group' is ambiguous for type lookup in this context
4. Group+CoreDataProperties.swift:43:82: 'SavedProfile' is ambiguous for type lookup in this context
推荐答案
我终于开始工作了.这是我所做的.(航班是我的实体之一)
I finally got mine to work. Here is what I did. (Flights is one of my entities)
我设置 xcdatamodeld 如下
I setup the xcdatamodeld as follows
然后实体为
然后我使用了 Editor -> Create NSManagedObject Subclass
Then I used Editor -> Create NSManagedObject Subclass
这会为我的航班实体创建两个文件
This creates two files for my flights entity
航班+CoreDataProperties.swift
Flights+CoreDataProperties.swift
航班+CoreDataClass.swift
Flights+CoreDataClass.swift
我将 Flights+CoreDataClass.swift 重命名为 Flights.swift
I renamed Flights+CoreDataClass.swift to Flights.swift
Flights.swift 只是
Flights.swift is just
import Foundation
import CoreData
@objc(Flights)
public class Flights: NSManagedObject {
}
Flights+CoreDataProperties.swift 是
Flights+CoreDataProperties.swift is
import Foundation
import CoreData
extension Flights {
@nonobjc public class func fetchRequest() -> NSFetchRequest<Flights> {
return NSFetchRequest<Flights>(entityName: "Flights");
}
@NSManaged public var ...
}
这似乎对我有用.我无法让 Codegen 以任何其他方式工作,即使我尝试了那里的许多建议.
This appears to work for me.I could not get Codegen to work in any other way, even though I tried many of the suggestions that were out there.
这也让我摸不着头脑,我将其添加为辅助.不要忘记使用 FetchRequest 的新泛型版本,您可以做到这一点
Also this had me scratching my head for a while and I add it as an assist. Don't forget with the new Generics version of the FetchRequest you can do this
let fetchRequest: NSFetchRequest<NSFetchRequestResult> = NSFetchRequest(entityName: "Flights")
这篇关于Xcode 8 为 iOS 10 生成损坏的 NSManagedObject 子类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!