在CFMutableDictionary中存储C结构 [英] Storing a C struct in CFMutableDictionary

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

问题描述

既然Core Foundation中没有NSValue的对应物,我们应该如何将C结构存储在CFMutableDictionary中?

Since there is no counterpart to NSValue in Core Foundation, how are we supposed to store C structs in a CFMutableDictionary?

推荐答案

首先,您可以按原样将NSvalue放入CFMutableDictionary中,因此答案是使用NSValue".但是我认为您剩下的问题是不使用任何可可对象".在这种情况下,只需创建一个非保留的CFMutableDictionary,然后就可以将任何指针放进去.请参阅定义自定义集合回调" ,以获取一些示例代码.我经常使用这些.

First, you can put an NSvalue in a CFMutableDictionary as-is, so the answer is "use NSValue." But I assume the rest of your question is "without using any Cocoa objects." In that case, just create a non-retaining CFMutableDictionary, and you can put any pointer you want into it. See "Defining Custom Collection Callbacks" for some example code. I use these a lot.

请记住,它们仍然必须是指针,因此您将不得不在堆而不是堆栈上分配结构.这意味着内存管理现在是您的问题.当然,您可以创建一组自定义的回调函数,以执行所需的任何操作,因此,如果您在使用boost或其他带有其自己的引用计数系统的东西时,仍可以使用CFMutableDictionary来实现.

Remember that these still have to be pointers, so you're going to have to allocate your structs on the heap, not the stack. And that means that memory management is now your problem. Of course you could create a custom set of callbacks that do whatever you want, so if you're using boost or something else with its own ref-counting system, you can still implement that with CFMutableDictionary.

当然,您可以用一个小的数据对象替换该结构.通常,这要容易得多.但是不同的问题需要不同的解决方案.

And of course you can replace the struct with a small data object. That's usually a lot easier. But different problems need different solutions.

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

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