将基本 CFTypeRef 转换为更具体的 CoreFoundation 类型
有没有一种简单的方法可以将 CTypeRef
转换为特定的 CoreFoundation 类型?我不想将内联转换为 (CFStringRef)myObjectRef
但想创建一个辅助方法来为我的所有 CoreFoundation 类型执行此操作。
我知道可以使用像 CFGetTypeID(myObjectRef) == CFStringGetTypeID() 之类的东西来确定 CTypeRef
是否是 CFString
。然而,创建单个方法来执行此操作可能会变得非常冗长并且有很多 if 语句。
使用针对 CFGetTypeID()
的大量 if 语句构建方法是唯一的方法吗?或者有更简单的方法来做到这一点吗?
更新:举个例子,
我想创建一个辅助函数来处理一些我无法更改的遗留代码。目前,它生成 CFDictionaryRef
、CFStringRef
或 CFURLRef
之一作为作为 CTypeRef
提供的返回值。我目前正在通过对返回值运行 CFGetTypeID()
来解决此问题,但这并不理想。我宁愿有一个 CastToCF()
帮助器来帮我处理这个问题,而不是到处都有 C 风格的强制转换。这也将有助于使未来的测试变得更加容易。
PS 我不担心可变类型。
Is there a simple way to convert a CTypeRef
to a specific CoreFoundation type? I'm not looking to cast inline as (CFStringRef)myObjectRef
but would like to create a helper method to do this for me for all CoreFoundation types.
I know it's possible to use something like CFGetTypeID(myObjectRef) == CFStringGetTypeID()
to find out whether a CTypeRef
is a CFString
. However creating a single method to do this could become very verbose and have a lot of if statements.
Is building out a method with a bunch of if statements against CFGetTypeID()
the only way? Or is there a simpler way to do this?
UPDATE: with example
I'd like to make a helper function to work with some legacy code I can't change. Currently it produces one of CFDictionaryRef
, CFStringRef
or CFURLRef
as a return value provided as a CTypeRef
. I'm currently working around this by running CFGetTypeID()
on the returned value but this isn't ideal. Rather than having C-style casts all over the place, I'd rather have a CastToCF()
helper which handles this for me. This would help make testing easier in the future as well.
P.S. I'm not worried about mutable types.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这样做没有明显的意义。 ac 风格的转换与其他语言不同 - 它是一种类型转换,左侧的地址与右侧的地址相同。如果您进行了错误的转换,cftypes 不会抛出或返回 null(与其他语言不同)。 iow,它只是您指定类型的装饰,并且 ac 编译器将假定您的强制转换是有效的。
或者也许您可以提供一个更好的示例来说明如何使用它(如果这没有帮助)。
更新
好的。既然您将其标记为 objc++,我只需创建一个辅助类,该类具有大量诊断功能并执行所有嘈杂的转换(最小说明):
这大约是我在没有您正在处理的程序的真正具体示例的情况下所能得到的最准确的结果和。
there's no obvious point in doing this. a c style cast is not like other languages - it is a typecast which the address on the left will be identical to the address on the right. cftypes will not throw or return null if you do a bad cast (unlike other languages). iow, it's merely a decoration for you to specify a type, and a c compiler will assume your cast is valid.
or perhaps you can provide a better example of how you would use this, if that did not help.
Update
ok. since you tagged it objc++, i'd just create a helper class which had plenty of diagnostics and did all the noisy conversions (minimal illustration):
that's about as accurate as i can get get without a really specific example of the program you are dealing with.