views:

1340

answers:

2

I have code like this:

NSData *data = [NSData dataWithContentsOfURL:objURL];
const void *buffer = [data bytes];
[self _loadData:buffer];
[data release];

the "_loadData" function takes an argument like:

- (void)_loadData:(const char *)data;

How do I convert "const void " to a "const char" on Objective-C?

+4  A: 

Just like you would in C:

[self _loadData:(const char *)buffer];

should work.

duncanwilcox
Thanks!I had tried assigning buffer to another type thinking "they're just pointers, it should work." const char *charBuff = buffer; Never thought to cast it. Ha!
Nick VanderPyle
That would have worked too actually, except "const char *charBuff = (const char *)buffer; (assigning to a char * variable, you still need the cast)
duncanwilcox
+3  A: 

You mustn't release the data object because you did not explicitly allocate it. Also, you could do a simple cast:

[self _loadData:(const char *) buffer];
dreamlax
Good catch on the NSData. I need to read more about memory management. Thanks!
Nick VanderPyle