如何在NSMutableArray中存储CGGradientRef [英] How to store CGGradientRef in NSMutableArray

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

问题描述

我在NSMutableArray中存储CGGradientRef的任务存在问题。
我需要将它包装到数组中,并将其存储到数组中以及解开它。



谢谢。

解决方案

您不必将其包裹在任何东西中。将它转换为 id 并将其添加到数组中。您可以对任何派生自CFType的CoreFoundation类型(松散地以Ref结尾的任何东西)执行此操作。

或者,您可以将NSMutableArray *强制转换为CFMutableArrayRef并调用CFArrayAppendValue ()。这不再是类型安全的;它会调用创建数组时指定的保留/释放回调。 (一个值得注意的异常例外是 - [NSDictionary setValue:forKey:]它复制键,而相应的CFDictionary函数不会。偶尔有一个字典由一个可变对象键入是很好的(例如NSURLConnection,带有数据-far as values);只要你不违反散列值不变的要求,你就是安全的。)

CoreFoundation类型类似OO可以在普通C中使用的类型。使用一些聪明的魔法(Apple可以这样做是因为它们编写了CF和ObjC运行时),像NSS​​tring,NSArray和NSDictionary类型的实例是(默认情况下)真正实例CFString,CFArray和CFDictionary。 CF类型位于类型层次结构中,其根源是 CFType ;值得注意的方法是CFRetain,CFRelease,CFEqual,CFHash和CFCopyDescription。这些松散地对应于-retain,-release,-isEqual:,-hash和-description(除了-description是另外自动释放的,并且它们具有不同的NULL / nil处理)。 Apple称之为免费桥接或可互换数据类型(尽管IDT文章已不在Mac开发中心中)。 Ridiculousfish有一篇名为Bridge的好文章。



这意味着您可以在大多数情况下将CF类型视为Objective-C对象。苹果公司并没有特别明显地表明这一点,但除了所有提及的免费桥接之外,还有一些假设它们可以互换的东西。


  • NSMakeCollectable()接受CFTypeRef并返回id(并启用垃圾收集,这与iOS无关,但与Mac OS 10.5 +高度相关)。
  • code> @property(nonatomic,retain)__attribute __((NSObject))ABAddressBookRef foo; 这实际上意味着发送它 - 保持和释放,就像它是一个Objective-C实例一样。

  • CALayer.contents属性的类型为 id ,但它支持的唯一记录的事情是CGImageRef(它也支持CABackingStore,这就是-drawRect:draws to)。


    你可以做的其他有趣的事情包括定义 CFutorelease


    I am having problem with the task of storing CGGradientRef in NSMutableArray. What do I have to wrapp it in to store it in the array and what to unwrap it.

    Thanks.

    解决方案

    You don't have to wrap it in anything. Just cast it to id and add it to the array. You can do this for any CoreFoundation type derived from CFType (loosely anything ending in "Ref").

    Alternatively, you can cast the NSMutableArray* to a CFMutableArrayRef and call CFArrayAppendValue(). This is no more typesafe; it calls the retain/release callbacks specified when the array is created. (A notable exception exception is -[NSDictionary setValue:forKey:] which copies keys, whereas the corresponding CFDictionary function does not. Occasionally it's nice to have a dictionary keyed by a "mutable" object (e.g. a NSURLConnection, with the data-so-far as values); you're safe as long as you don't violate the requirement that the hash doesn't change.)

    CoreFoundation types are OO-like types that can be used in "plain" C. Using some clever magic (Apple can do this because they write both the CF and ObjC runtimes), instances of types like NSString, NSArray, and NSDictionary are (by default) "really" instances of CFString, CFArray, and CFDictionary. CF types are in a type hierarchy, the root of which is CFType; the notable methods are CFRetain, CFRelease, CFEqual, CFHash, and CFCopyDescription. These loosely correspond to -retain, -release, -isEqual:, -hash, and -description (except that -description is additionally autoreleased, and they have different handling of NULL/nil). Apple calls this "toll-free bridging" or "Interchangeable Data Types" (the IDT article is no longer on the Mac Dev Center, though). Ridiculousfish has a good article called "Bridge".

    What this means is that you can, for the most part, treat CF types as if they were Objective-C objects. Apple hasn't made this particularly obvious, but in addition to all the mentions of "toll-free bridging", there's some stuff that assumes they're interchangeable.

    • NSMakeCollectable() takes a CFTypeRef and returns id (and enables garbage collection, which is irrelevant on iOS but highly relevant on Mac OS 10.5+).
    • Stuff like @property (nonatomic, retain) __attribute__((NSObject)) ABAddressBookRef foo; which effectively means "send it -retain and -release as if it were an Objective-C instance".
    • The CALayer.contents property has type id but the only documented thing it supports is CGImageRef (it also supports CABackingStore, which is what -drawRect: draws to).

    Additional fun things you can do include defining CFAutorelease.

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

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