问题描述
我最近更新了我的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.
核心数据模型的最低工具版本设置为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给出的具体错误: / p>
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子类
Then I used Editor -> Create NSManagedObject Subclass
航班+ 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 {
}
航班+ 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 ...
}
$ b b
这似乎对我工作。我不能让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子类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!