Re: Guidance for learner
Re: Guidance for learner
- Subject: Re: Guidance for learner
- From: "Daniel T. Staal" <email@hidden>
- Date: Wed, 27 Jun 2007 13:45:09 -0400 (EDT)
- Importance: Normal
On Wed, June 27, 2007 11:04 am, Barry said:
> int i=1;
> NSData *data=[[MyDocController dataFiles] objectAtIndex: i];
> const char *p;
> p=[data bytes];
>
> I'm sure you can see a problem.
> One: Does dataFiles (the NSArray) know that it's Objects are NSData
> Objects
> ?
> If not, how do I coerce it (sorry, using C++ terminology here)
It neither knows nor cares. Neither do you: this is Obj-C, it is possible
to pass an object without specifying at compile-time what class it is.
The above code will take an object, and assume it is a NSData object,
treating it as such. If it isn't, you'll get a runtime error when you
pass it a method it doesn't support. If it supports all the methods you
call on it, the code will run without errors or warnings. _Even if it
isn't an NSData object you get._
Coercion is rarely needed in Obj-C, for anything. In fact, if you find
yourself trying to use it, you are probably doing something wrong.
> Similarly, [data bytes] returns a void* which needs coercing to byte*
void* and byte* are C data types: Use the same methods you would use in C.
Obj-C does not invalidate any of C. In this case, void* is just a
pointer to a section of memory, and NSData is saying it can't garuntee
anything about the structure of the data. It may be a stream of bytes, or
it may not.
Daniel T. Staal
---------------------------------------------------------------
This email copyright the author. Unless otherwise noted, you
are expressly allowed to retransmit, quote, or otherwise use
the contents for non-commercial purposes. This copyright will
expire 5 years after the author's death, or in 30 years,
whichever is longer, unless such a period is in excess of
local copyright law.
---------------------------------------------------------------
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden