Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Safest way to convert CFDataRef to NSData object

569 views
Skip to first unread message

mario

unread,
Feb 2, 2009, 2:28:37 AM2/2/09
to
Hello all,

What is the safest way to convert CFDataRef variable to a NSData
object?

Would it be as straightforward as this:
NSData* dataVal = (NSData*) data; //where data is a CFDataRef variable

My concern is that maybe the conversion might truncate the exact value
of CFDataRef variable that would produce unexpected results.

Thanks.

Mario

Gregory Weston

unread,
Feb 2, 2009, 9:35:02 AM2/2/09
to
In article
<8d3bbbc0-5d3a-4b9a...@v42g2000yqj.googlegroups.com>,
mario <mario...@gmail.com> wrote:

> Hello all,
>
> What is the safest way to convert CFDataRef variable to a NSData
> object?
>
> Would it be as straightforward as this:
> NSData* dataVal = (NSData*) data; //where data is a CFDataRef variable

From the NSData docs:

'NSData is "toll-free bridged" with its Core Foundation counterpart,
CFData. This means that the Core Foundation type is interchangeable in
function or method calls with the bridged Foundation object. Therefore,
in a method where you see an NSData * parameter, you can pass a
CFDataRef, and in a function where you see a CFDataRef parameter, you
can pass an NSData instance (you cast one type to the other to suppress
compiler warnings). This also applies to your concrete subclasses of
NSData. See Interchangeable Data Types for more information on toll-free
bridging.'

> My concern is that maybe the conversion might truncate the exact value
> of CFDataRef variable that would produce unexpected results.

Doesn't make sense. NSData* is explicitly a pointer. CFDataRef is a
reference (which is also a pointer). Assignment, casting and the like
don't touch the contents of the object; just the thing you've got that
points to it.

--
"Harry?" Ron's voice was a mere whisper. "Do you smell something ... burning?"
- Harry Potter and the Odor of the Phoenix

David Phillip Oster

unread,
Feb 2, 2009, 10:27:39 PM2/2/09
to

There is a fine point if you want your source code to work both with
Garbage Collection on and with Garbage Collection off.

See
http://code.google.com/p/google-toolbox-for-mac/source/browse/trunk/Found
ation/GTMGarbageCollection.h for details, particularly look at the way
the open source toolbox uses GTMNSMakeCollectable()

0 new messages