How to store CGRect and other things in core data(如何在核心数据中存储 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,并且在最简单的情况下利用撤消/重做.问题是,我看到的所有示例都倾向于存储字符串或整数.如果我有一个想要在核心数据中实现的类(一个虚构的例子)怎么办:
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
}
这些都是当场编造出来的,但每一个都凸显了一个困惑
如我所见,我将打开 .xcdatamodel 并添加一个名为Badge"的新实体,它是一个 NSManagedObject
.然后,我为 textForBadge
添加一个 String
类型的属性.到现在为止还挺好.我为 badgeValue 做了类似的事情,但后来来到 UIColor
和 CGRect
我有点难过,因为没有这些属性.我是否应该创建一个实体来表示每个整数(即具有四个属性 x,y,w,h
的 Rect
实体)?然后每次都用这些整数填充一个 CGRect ?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?
最后,我来看看我的奖项
列表.如果这些是指向代表奖项的多个对象的指针列表,它们可能包含图像、颜色、文本等.我假设 award
将再次成为我必须设计的实体,而不是Badge
存储一个数组我会从它到 Award
类有一对多的关系.
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.
我做对了这件事,还是朝着相反的方向前进?我看到的所有示例都在像 String 或 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.
亲切的问候,
布林
推荐答案
这是相关文档: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 和其他东西的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在核心数据中存储 CGRect 和其他东西
基础教程推荐
- Android:对话框关闭而不调用关闭 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01