我的xcdatamodeld中有六个实体。文章,部分,参考,书签,书签参考和书签部分。我试图将书签文章单独保存到书签实体。文章具有与章节和引用相反的逆设置书签与BookmarkReferences和BookmarkSections的逆设置。当我尝试将文章NSManagedObject中的值设置为书签NSManagedObj时,我收到KVC错误。我确保密钥名称中没有拼写错误。看看代码和日志。
extension Bookmarks {
@nonobjc public class func fetchRequest() -> NSFetchRequest<Bookmarks> {
return NSFetchRequest<Bookmarks>(entityName: "Bookmarks");
}
@NSManaged public var id: String?
@NSManaged public var status: String?
@NSManaged public var createdBy: String?
@NSManaged public var articleType: String?
@NSManaged public var modifiedBy: String?
@NSManaged public var tag: String?
@NSManaged public var sendNotification: Bool
@NSManaged public var bookmark: Bool
@NSManaged public var isRead: Bool
@NSManaged public var isArticleFromSearch: Bool
@NSManaged public var searchString: String?
@NSManaged public var isArticleFromCategory: Bool
@NSManaged public var modifiedDate: String?
@NSManaged public var createdDate: String?
@NSManaged public var category: String?
@NSManaged public var subCategory: String?
@NSManaged public var title: String?
@NSManaged public var general: NSDictionary?
@NSManaged public var bookmarkSections: NSSet?
@NSManaged public var bookmarkReferences: NSSet?
}
extension BookmarkSections {
static func saveSectionsForBookmarks(entity: Article, privateContext: NSManagedObjectContext) -> NSSet {
let sections = entity.sections
let sectionED = NSEntityDescription.entity(forEntityName: "BookmarkSections", in: privateContext)
let sectionMO = NSManagedObject(entity: sectionED!, insertInto: privateContext)
let sectionsSet = NSMutableSet()
let allSectionItems = sections?.allObjects
for (_, sectionItem) in (allSectionItems?.enumerated())! {
let castedSection = sectionItem as? Sections
sectionMO.setValue(castedSection!.id, forKey: "id")
sectionMO.setValue(castedSection!.text, forKey: "text")
sectionMO.setValue(castedSection!.imageUrl, forKey: "imageUrl")
sectionMO.setValue(castedSection!.imageName, forKey: "imageName")
sectionMO.setValue(castedSection!.title, forKey: "title")
sectionsSet.add(sectionMO)
}
return sectionsSet
}
}
[`
extension Bookmarks {
public static func saveToNew(Entity: Article) {
let managedContext = CoreDataStorage.sharedInstance.privateQueueContext
let entityDesc =
NSEntityDescription.entity(forEntityName: "Bookmarks",
in: managedContext)!
let articleMO = NSManagedObject(entity: entityDesc,
insertInto: managedContext)
articleMO.setValue(Entity.id, forKey: "id")
articleMO.setValue(Entity.status, forKey: "status")
articleMO.setValue(Entity.createdBy, forKey: "createdBy")
articleMO.setValue(Entity.articleType, forKey: "articleType")
articleMO.setValue(Entity.modifiedBy, forKey: "modifiedBy")
let isSendNotification=NSNumber(value: false)
articleMO.setValue(isSendNotification, forKey: "sendNotification")
let modifiedDate = String(describing: Entity.modifiedDate)
articleMO.setValue(modifiedDate, forKey: "modifiedDate")
let createdDate = String(describing: Entity.createdDate)
articleMO.setValue(createdDate, forKey: "createdDate")
articleMO.setValue(Entity.category, forKey: "category")
articleMO.setValue(Entity.subCategory, forKey: "subCategory")
let general = NSMutableDictionary()
let dict = Entity.general
for (key,subJson):(Any, Any) in dict! {
general.setValue(subJson as! String, forKey: key as! String)
}
articleMO.setValue(general, forKey: "general")
articleMO.setValue(Entity.title, forKey: "title")
articleMO.setValue(BookmarkSections.saveSectionsForBookmarks(entity: Entity, privateContext: managedContext), forKey: "bookmarkSections")
articleMO.setValue(Entity.references, forKey: "references")
do {
try managedContext.save()
} catch let error as NSError {
print("Could not save. \(error), \(error.userInfo)")
}
}
`]
错误: