从 CoreData 检索基于 C 的结构

发布于 2024-11-03 17:17:54 字数 670 浏览 1 评论 0原文

我一直在开发一个使用C库的项目,我的项目需要在coredata中存储一些c结构。我怎样才能通过不将其转换为目标 c 对象来做到这一点。

我想存储一个 Opencv 结构(CvMat*)。

/* binding stuff example*/
CvMat *mat = cvCreateMat(3, 3, CV_32FC1);
// fill the matrix..
human.projVal = [NSData dataWithBytes:mat length:sizeof(mat)];
// then store it in core data
..  


/* retrieving stuff */
..
NSData *data = [[humansArray objectAtIndex:0] projVal];     
CvMat *mat = (CvMat *)[data bytes];
NSLog(@"cols =>> %i rows =>> %i",mat->cols,mat->rows);

for (int i=0; i<9; i++) {   
    NSLog(@"%f",mat->data.fl[i]);
}

当我尝试从 coredata 检索 cvMat 结构时,我得到奇怪的列和行值以及数据值。我不明白有什么问题。

提前致谢..

I have been working on a project that uses C libraries, and my project needs to store some c structs in coredata. How can I do this by not transforming it to objective c object.

I want to store an Opencv struct(CvMat*).

/* binding stuff example*/
CvMat *mat = cvCreateMat(3, 3, CV_32FC1);
// fill the matrix..
human.projVal = [NSData dataWithBytes:mat length:sizeof(mat)];
// then store it in core data
..  


/* retrieving stuff */
..
NSData *data = [[humansArray objectAtIndex:0] projVal];     
CvMat *mat = (CvMat *)[data bytes];
NSLog(@"cols =>> %i rows =>> %i",mat->cols,mat->rows);

for (int i=0; i<9; i++) {   
    NSLog(@"%f",mat->data.fl[i]);
}

when I try to retrieve the cvMat struct from coredata, I get weird cols and rows value and also data values. I cannot understand what is the problem.

Thanks in advance..

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

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

发布评论

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

评论(2

放肆 2024-11-10 17:17:54

您可以将其存储为基础值并重新创建它。

或者,您可以将其存储为“可转换”属性。 Core Graphics 对于各种结构类型(例如 CGRect)有类似的东西。在数据模型中将类型标记为“Transformable”,然后在实体类中显式创建 getter 和 setter。

您需要创建像 Core Graphics 一样将属性转换为字符串的函数,例如 CGRectFromString 和 NSStringFromCGRect。

如果您仍然想将其存储为二进制数据,则可以使用以下链接中描述的类似方法对其进行编码和解码:如何在 Coredata 中使用 struct 存储自定义对象

You could store it as the underlying values and the re-create it.

Alternatively you could store it as a "Transformable" attribute. Core Graphics has something similar for various struct types such as CGRect. In the data model mark the type as "Transformable", then explicitly create the getters and setters in your entity class.

You'll need to create functions that transform the properties to a string as Core Graphics does, e.g. CGRectFromString and NSStringFromCGRect.

If you still want to store it as binary data, you may have more success encoding and decoding it using something like this described here in this link: How to store custom objects with struct in Coredata.

冷默言语 2024-11-10 17:17:54

...我的项目需要存储一些c
coredata 中的结构体。我怎样才能做到这一点
不将其转换为目标 c
对象。

你确实无法可靠地做到这一点。核心数据存储对象,而结构不是对象。原则上,您可以将结构转换为数据对象,但由于不同硬件解释数据的方式,这是一个脆弱的解决方案。如果有人将存储移动到另一台设备,结构可能会有所不同。

最安全的解决方案是简单地编写一个转换器,将结构转换为对象值,然后再转换回来。假设您有一个包含两个 int 值的结构。您可以将每个 int 转换为 NSNumber,将它们放入数组中,然后将数组作为存档存储在数据属性中。或者,您可以创建一个模仿该结构的实体。我更喜欢后一种方法。

...my project needs to store some c
structs in coredata. How can I do this
by not transforming it to objective c
object.

You really can't do it reliably. Core Data stores objects and structs aren't objects. In principle, you can convert structs to data objects but that is a fragile solution owing to the way that different hardware interprets the data. If someone moves the store to another device, the structs may come out differently.

The safest solution is to simply write a transformer that converts the structs to object values and then back again. Say you had a struct with two int values. You would convert each int into a NSNumber, put those into an array and then store the array as an archive in a data attribute. Alternatively, you can create an entity that mimics the struct. I prefer the latter method.

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