如何映射NSManageBject的模型?

发布于 2025-02-04 03:46:22 字数 883 浏览 3 评论 0原文

当我尝试执行此操作时,如果我使用上下文,则该模型将存储在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
}

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

北风几吹夏 2025-02-11 03:46:22

考虑重新设计您的模型以计算出新的包装器类型的属性,该属性会转换属性值与包装器值的属性值。

在Swift Core数据模型中实施计算的属性通常是实现所需物品的一种清晰,更直观的方法。

这是一个示例实现:

struct WordPresentation {
    let word: String
    let uuid: UUID }

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?
    
    var wordPresentation : WordPresentation {
        get {
            return WordPresentation(word: self.word, uuid: self.uuid)
        }
        set {
            self.word = newValue.name
            self.uuid = newValue.id
        }
    } 
}

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:

struct WordPresentation {
    let word: String
    let uuid: UUID }

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?
    
    var wordPresentation : WordPresentation {
        get {
            return WordPresentation(word: self.word, uuid: self.uuid)
        }
        set {
            self.word = newValue.name
            self.uuid = newValue.id
        }
    } 
}
暖阳 2025-02-11 03:46:22

我解决了这样的问题(我不知道为什么要推迟它并且不立即理解):

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 removeFromStorage(by uuid: UUID) {
    getDataFromStorage { [weak self] objects  in
        guard let self = self else { return }
        if let objectForRemove = objects.first(where: { $0.uuid == uuid }) {
        self.coreDataStack.mainContext.delete(objectForRemove)
        self.coreDataStack.saveContext(self.managedObjectContext)
        }
    }
}

我正在使用UUID创建演示级模型!
我只删除他自己。
现在我可以双向行走。

I solved the problem like this (I don't know why I put it off and didn't understand right away):

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 removeFromStorage(by uuid: UUID) {
    getDataFromStorage { [weak self] objects  in
        guard let self = self else { return }
        if let objectForRemove = objects.first(where: { $0.uuid == uuid }) {
        self.coreDataStack.mainContext.delete(objectForRemove)
        self.coreDataStack.saveContext(self.managedObjectContext)
        }
    }
}

I'm creating a presentation level model with UUID!
And I delete only on him himself UUID.
Now I can walk both ways.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文