NSManagedObject子类作为管理对象外部的正常对象 [英] NSManagedObject subclass outside of managed object as a normal object

查看:337
本文介绍了NSManagedObject子类作为管理对象外部的正常对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个实体物件国家/地区具有国家/地区名称和国家/地区代码。它是NSManagedObject的子类,我使用它与核心数据模型来存储它的值到持久存储。

I have an entity object Country with country name and country code. It is a subclass of NSManagedObject and I am using it with core data model to store its value to a persistent store.

我有一个地方,相同的国家对象将使用作为正常对象,即我将使用它来存储一些临时国名。

I have a place where the same Country object will used as a normal object i.e. I will use it to store some temporary country name.

为此,我已将国家/地区初始化为以下

For that I have initialized the Country as following

[NSManagedObject alloc] init]


$ b $ p

初始化成功完成,但是我不能为该对象设置任何属性。
因此我做了一个探索。在那里我发现,NSManagedObject的init不支持按照文档。

Initialization successfully done, but I am not able to set any property to that object. Hence I did an exploration. In that I found that, init for the NSManagedObject is not supported as per the documentation.

我不知道如何使用NSManagedObject国家与CoreData以及一个普通的对象。

I dont know how to use the NSManagedObject Country with CoreData as well as a normal Object.

如果有可能,请让我知道解决方案,因为我迫切需要这个asap ..

If there is a possibility, please let me know the solution as I am in desperate need of this asap..

提前感谢。

推荐答案

NSManagedObject类文档概述的第2段:

2nd paragraph of the NSManagedObject class documentation's overview:


托管对象与
实体描述(
NSEntityDescription的一个实例)相关联,该实体提供
关于该对象的元数据(包括
对象
表示的实体的名称以及其
属性和关系的名称)和
a管理对象上下文(跟踪
)更改为对象图。
重要的一个托管对象是
正确配置为与Core
数据一起使用。 如果直接实例化托管
对象,您必须调用
指定的初始化程序
(initWithEntity:insertIntoManagedObjectContext:)。

从方法的文档:


重要 :此方法是
指定的初始化程序为
NSManagedObject。您不应该
初始化一个托管对象只需
发送它init。

Important: This method is the designated initializer for NSManagedObject. You should not initialize a managed object simply by sending it init.

文档实际上很好。

您不想尝试在可行的CoreData堆栈之外使用NSManagedObject。 NSManagedObject被明确设计为在正确配置的Core Data环境中工作。如果您需要一个临时实例,您可以创建一个内存存储或在您的常规存储中创建一个,只是不保存更改,而不先删除它。

You do not want to try to use an NSManagedObject outside of a viable CoreData stack. NSManagedObjects are quite explicitly designed to work within a correctly configured Core Data environment. If you need a temporary instance, you can either create an in-memory store or create one in your regular store and just don't save the changes without deleting it first.

这篇关于NSManagedObject子类作为管理对象外部的正常对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆