lajos
lajos

Reputation: 25715

how to add opaque types to collections

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];

Upvotes: 0

Views: 340

Answers (2)

lajos
lajos

Reputation: 25715

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];

Upvotes: 0

Martin Gordon
Martin Gordon

Reputation: 36389

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.

Upvotes: 3

Related Questions