NSArray与C类型? [英] NSArray with C types?

查看:117
本文介绍了NSArray与C类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否可以使用带有C类型的NSArray / NSMuatbleArray作为元素,即CGLayerRef?

I am wondering whether it's possible to use an NSArray/NSMuatbleArray with C types as elements, namely CGLayerRef?

如果将CGLayerRef对象添加到NSMutableArray,则代码编译但是抛出警告:

If adding CGLayerRef objects to the NSMutableArray the code compiles but a warning is thrown:


警告:传递
'objectForKey:'的参数1从
整数生成指针演员

warning: passing argument 1 of 'objectForKey:' makes pointer from integer without a cast

因此我的问题:如何在可变数组中存储C类型?

Thus my question: How can I store C types in a mutable array?

最好,

heinrich

推荐答案

这是多部分答案。最后,答案就是你可以做到这一点。

This is a multipart answer. In the end, the answer for you is going to be "you can do this".

(1)NSArray无法处理一般的C类型。如果你对这种事情感兴趣,CFArray可以配备自定义回调来存储其他类型的数据。警告:通常你可以将CFArray传递给任何采用API的NSArray - 它们是桥接的。这不适用于具有自定义回调的CFArrays。

(1) NSArray cannot handle general C types. If you're interested in this kind of thing, CFArray can be outfitted with custom callbacks for storing other kinds of data. Caveat: Normally you can pass a CFArray to any NSArray taking API - they're bridged. This does not apply to CFArrays with custom callbacks.

(2)CGLayerRef不是任何旧的C类型,它是CFType。 CFType桥接到NSObject。向任何CFType发送 -retain -release 就像在NSObject上一样。如果您要在NSObject上实现一个类别,实现 -foo ,然后将一些随机CFType转换为id并向其发送-foo消息,您将看到<$ $的实现c $ c> foo 已调用。

(2) CGLayerRef is not any old C type, it's a CFType. CFType is bridged to NSObject. Sending -retain and -release to any CFType works just as it would on an NSObject. If you were to put a category on NSObject implementing -foo and then cast some random CFType to id and send it the -foo message, you'd see your implementation of foo invoked.

因此,此处的编译器警告是唯一的问题。您可以强制转换为(id)以避免它。支持所有这些。

So, the compiler warning here is the only problem. You can cast to (id) to avoid it. All of this is supported.

Ken Ferry

Ken Ferry

Cocoa Frameworks

Cocoa Frameworks

这篇关于NSArray与C类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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