如何在核心数据中存储CGRect和其他东西 [英] How to store CGRect and other things in core data

查看:124
本文介绍了如何在核心数据中存储CGRect和其他东西的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些与coredata的早期问题,但觉得如果有人可以解释一些简单的案件给我,我会很清楚的东西。

I'm having a few teething problems with coredata, but feel it would clear things up for me greatly if someone could explain some simple cases to me.

我想把我的模型放入coredata,在一个最简单的情况下利用undo / redo。事实是,我看到的所有示例都倾向于存储字符串或整数。如果我有一个类如下,我想在核心数据(一个组成的例子)实现:

I want to put my model into coredata, and at a most simple case take advantage of undo/redo. Thing is, all the examples I see tend to store either strings or integers. What if I have a class as follows that I wanted to implement in core data (a made up example):

@interface Badge : NSObject {

NSString *textForBadge;
int      badgeValue;
UIColor  *color;
CGRect    rect;
NSMutableArray *awards; // this would be a list of 'Category' - another custom class
}

em>

These are all made up on the spot, but each highlight a confusion

在我看来,我会打开.xcdatamodel并添加一个新的实体已归档徽章,这将是一个 NSManagedObject 。然后为类型 String textForBadge 添加一个属性。到现在为止还挺好。我做了一些类似的badgeValue,但然后来到 UIColor CGRect ,我有点郁ped,因为没有一个属性。我应该创建一个实体来代表每个(即 Rect 实体有四个属性 x,y,w,h )是ints?然后填充一个CGRect与这些ints每次?同上UIColor?

As I see it, I would open the .xcdatamodel and add a new Entity caled 'Badge', which would be an NSManagedObject. I then add a property for textForBadge of type String. So far so good. I do somthing similar for badgeValue, but then come to the UIColor and CGRect and I'm a bit stumped, since there isn't a property for those. Am I supposed to create an entity to represent each (i.e a Rect entity that has four properties x,y,w,h) that are ints? Then populate a CGRect with these ints each time? Ditto for the UIColor?

最后,我来到我的 awards 列表。如果这些是指向表示奖励的多个对象的指针列表,则它们可以包含图像,颜色,文本等。我​​假设将再次是实体I必须设计,而不是 Badge 存储数组我将有一个从1到许多关系到类。

Finally, I come to my list of awards. If these are a list of pointers to a number of objects representing an award they might contain an image, a colour, text etc. I assume that award would again be an entity I have to design and rather than Badge storing an array I would have a 1 to many relationship from it to the Award class.

我有没有这个权利,或者在相反的方向?所有的例子,我看到操作对象像字符串或int的香草对象,所以要确保我有我的头在实现一堆东西之前。

Am I getting any of this right, or going in the compete opposite direction? All the examples I see operate on vanilla objects like String or int so want to make sure I have this right in my head before implementing a bunch of stuff.

Bryn

推荐答案

这是相关文档: http://developer.apple.com/library/mac/#documentation/Cocoa /Conceptual/CoreData/Articles/cdNSAttributes.html ,但实质上,您必须为Core Data提供一种在非标准属性和它可以存储的事物之间进行转换的方法。有两种方法可以做到。要么使用可变属性和值变换器,要么使用临时属性,并且只有当被保存对象上下文被保存时才进行转换。

This is the relevant documentation: http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CoreData/Articles/cdNSAttributes.html but essentially, you have to provide a way for Core Data to translate between your non-standard properties and the kinds of things it can store. There are two ways to do it. Either use transformable attributes and value transformers, or use transient properties and do the conversion only when the managed object context is going to be saved.

我认为在大多数情况下,你描述(定义自定义实体来存储像CGRect这样的东西的组件)。

I think in most cases, what you describe (defining custom entities to store the components of something like a CGRect) is overkill.

这篇关于如何在核心数据中存储CGRect和其他东西的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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