在Sqlite中存储NSMutableArray [英] Storing NSMutableArray in Sqlite

查看:87
本文介绍了在Sqlite中存储NSMutableArray的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个NSMutableArray,包含多个CGRects的坐标和大小。我想将它存储到sqlite中。是否可以存储整个阵列?或者我应该手动将CGRects存储在数据库中吗?

I have an NSMutableArray containing the coordinates and sizes of multiple CGRects. I want to store this into the sqlite. Is it possible to store the whole array? Or should I just store the CGRects manually in the database?

更新:

这是我从数据库中检索到的数据:

This one is the data I retrieved from the database:

62706c69 73743030 d4010203 04050817 18542474 6f705824 6f626a65 63747358 24766572 
73696f6e 59246172 63686976 6572d106 0754726f 6f748001 a3090a0f 55246e75 6c6cd20b 
0c0d0e5a 4e532e6f 626a6563 74735624 636c6173 73a08002 d2101112 16582463 6c617373 
65735a24 636c6173 736e616d 65a31314 155e4e53 4d757461 626c6541 72726179 574e5341 
72726179 584e534f 626a6563 745e4e53 4d757461 626c6541 72726179 12000186 a05f100f 
4e534b65 79656441 72636869 76657208 11161f28 32353a3c 40464b56 5d5e6065 6e797d8c 
949dacb1 00000000 00000101 00000000 00000019 00000000 00000000 00000000 000000c3

它与我放入数据库之前的之前的完全相同。

It is exactly the same as the one before I put inside the database.

代码:

这是我用来检查NSArray是否存档的代码。我还没有将它放在数据库中,因为我只想确保NSKeyedUnarchive可以工作。这是:

This is the code I am using to check if the NSArray is being archived. I am not putting it in a database yet, because I just want to make sure that the NSKeyedUnarchive works. Here it is:

    NSData *toData = [NSKeyedArchiver archivedDataWithRootObject:rectsArray];
    NSLog(@"The data %@", toData);
    NSArray *fromData = [NSKeyedUnarchiver unarchiveObjectWithData:toData];
    NSLog(@"The data %@", fromData);

toData 显示已存档的表单,但 fromData 根本没有显示任何内容。为什么会这样?

The toData shows the archived form, but the fromData shows nothing at all. Why is this happening?

以下是的示例rectsArray

(
"NSRect: {{83.389008, 179.61432}, {8.775116, -10.616217}}",
"NSRect: {{83.389008, 179.61432}, {8.775116, -10.616217}}",
"NSRect: {{83.389008, 179.61432}, {8.775116, -10.616217}}",
"NSRect: {{96.085426, 179.61432}, {5.2029028, -10.616217}}",
"NSRect: {{101.28833, 179.61432}, {5.8532658, -10.616217}}",
"NSRect: {{107.1416, 179.61432}, {4.5525398, -10.616217}}",
"NSRect: {{115.61544, 179.61432}, {3.2518141, -10.616217}}",
"NSRect: {{115.61544, 179.61432}, {3.2518141, -10.616217}}",
"NSRect: {{118.86726, 179.61432}, {6.5036283, -10.616217}}",
"NSRect: {{118.86726, 179.61432}, {6.5036283, -10.616217}}"
)

如你所见,它是一个CGRects数组。

As you can see, it is an array of CGRects.

推荐答案

好的 - 使用Core Data这有点容易,但基础是相同的。您需要使用存档将数组数据存储到NSData对象中:

Ok--this is a bit easier with Core Data, but the basics are the same. You need to store the array data into an NSData object using an archive:

NSData *dataFromArray = [NSKeyedArchiver archivedDataWithRootObject:myArray];

然后将该数据保存为数据库中的blob。根据您使用的SQLite包装器,它可能有一个方法获取NSData对象并将其转换为字节,否则您可以使用以下内容获取原始字节:

And then save that data as a blob in the database. Depending on the SQLite wrapper you use, it may have a method taking the NSData object and translating it into bytes for you, otherwise you can get the raw bytes with:

const char *arrayBytes = [dataFromArray bytes];

要获取数据,您可以将数据库中的字节返回到NSData对象中,然后解开它。假设您可以从数据库中获取blob并将其放入名为dataFromDB的NSData对象中,您将执行以下操作:

To get the data back out, you get the bytes from the database back into an NSData object and then unarchive it. Assuming that you can get your blob out of the database and into an NSData object called dataFromDB, you will do:

NSArray *myArrayFromDB = [NSKeyedUnarchiver unarchiveObjectWithData:dataFromDB];

你将恢复你的阵列!

这篇关于在Sqlite中存储NSMutableArray的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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