views:

116

answers:

2

How can I add an opaque type to a collection in cocoa?

I get a compiler warning for this (obviously, because opaque types are not objects):

CGColorSpaceRef colorSpace;
NSArray *myArray = [NSArray arrayWithObject:colorSpace];
+3  A: 

You can use the NSValue class to wrap your opaque types in an object. From here:

An NSValue object is a simple container for a single C or Objective-C data item. It can hold any of the scalar types such as int, float, and char, as well as pointers, structures, and object ids. The purpose of this class is to allow items of such data types to be added to collections such as instances of NSArray and NSSet, which require their elements to be objects. NSValue objects are always immutable.

Martin Gordon
This is not the correct answer. CFTypes can be directly added to collections through toll free bridging. See accepted answer.
lajos
A: 

CoreFoundation data types (CFTypes) can be directly added to collections. (They need to be cast to (id) to suppress warnings.) This is called "toll free bridging."

CGColorSpaceRef colorSpace;
NSArray *myArray = [NSArray arrayWithObject:(id)colorSpace];
lajos