如何映射NSManageBject的模型?
当我尝试执行此操作时,如果我使用上下文,则该模型将存储在NSManageBjectContext中,并且没有它会引发错误,但是我并不期望相同的结果。 有没有简单的方法来实施此功能?
class WordDal: NSManagedObject {
@nonobjc public class func fetchRequest() -> NSFetchRequest<WordDal> {
return NSFetchRequest<WordDal>(entityName: "WordDal")
}
@NSManaged public var word: String?
@NSManaged public var uuid: UUID?
}
struct WordPresentation {
let word: String
let uuid: UUID
}
func mappingNSManagedObject(_ wordPresentation: WordPresentation) -> WordDal {
let model = WordDal()
model.uuid = wordPresentation.uuid
model.word = wordPresentation.word
return model
}
When I try to do this, the model is stored in the NSManagedObjectContext if I use the context, and without it it throws an error, but I'm not expecting the same result.
Is there an easy way to implement this?
class WordDal: NSManagedObject {
@nonobjc public class func fetchRequest() -> NSFetchRequest<WordDal> {
return NSFetchRequest<WordDal>(entityName: "WordDal")
}
@NSManaged public var word: String?
@NSManaged public var uuid: UUID?
}
struct WordPresentation {
let word: String
let uuid: UUID
}
func mappingNSManagedObject(_ wordPresentation: WordPresentation) -> WordDal {
let model = WordDal()
model.uuid = wordPresentation.uuid
model.word = wordPresentation.word
return model
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
考虑重新设计您的模型以计算出新的包装器类型的属性,该属性会转换属性值与包装器值的属性值。
在Swift Core数据模型中实施计算的属性通常是实现所需物品的一种清晰,更直观的方法。
这是一个示例实现:
Consider to redesign your model to have computed property for the new wrapper type that transforms the property value to and from the wrapper value.
Implementing a computed property in a Swift Core Data model is often a clear, more intuitive way to achieve what you need.
Here is an example implementation:
我解决了这样的问题(我不知道为什么要推迟它并且不立即理解):
我正在使用UUID创建演示级模型!
我只删除他自己。
现在我可以双向行走。
I solved the problem like this (I don't know why I put it off and didn't understand right away):
I'm creating a presentation level model with UUID!
And I delete only on him himself UUID.
Now I can walk both ways.