understanding objective c

zots

Re-member
im trying to understand the syntax of objective c. right now i'm doing the cocoa tutorials on oreillynet
http://www.oreillynet.com/pub/a/mac/2001/06/01/cocoa.html?page=2

my question is about this method declaration:
Code:
- (NSData *)dataRepresentationOfType:(NSString *)aType
can someone break this down for me? what i understand is the method will return a variable of data type NSString. i dont understand (NSData *) before the method, what * indicates, or what aType is.

thanks
 
- (NSData *)dataRepresentationOfType:(NSString *)aType

Breakdown:
(NSData *) This is the return type. The * means that it's a pointer to an NSData. The difference between a pointer and just a variable is a pretty big topic though. Reading some normal C tutorials would probably explain that. In Objectice-C though, staticly allocated objects (which would be just (NSData) ) aren't allowed, so you need the '*'s pretty much everywhere. Bottom line though is IMHO you should get comfortable with generic C before you dig too deep in objective-C - it will make it all a lot easier.

dataRepresentationOfType: This is just the name of the method.

(NSString*) This says that the argument to the method is a pointer to an NSString

aType is the name of the variable that get's passed in - so when you called this method, like

theDataIWant = [class dataRepresentationOfType:mad:"this is the string that I'm passing in"];

you'd refer to that string by the name aType inside of the dataRepresentationOfType method.
 
Incidentally, NSData is an object wrapper for an arbitrary collection of bytes. It's about as generic an object as you can get, and is often used when unknown types of data will be exchanging hands.

-Rob
 
- (NSData *)dataRepresentationOfType:(NSString *)aType

hehe i was wondering how that :( got there. But ill try and explain it in English.

- (NSData *)dataRepresentationOfType:(NSString *)aType


What you should really learn first is objects. Everything will click as soon as you learn about objects. The line above is what you call a "method." - The only way to access an object. It actually returns the NSData type. The NSString is simply telling the compiler what "aType" is. dataRepresentationOfType: is the method.

- (NSData *)dataRepresentationOfType:(NSString *)aType

Is actually the method definition.

To actually perform a method you would do this:

NSString * myString;
NSData * myData;

[myData dataRepresentationOfType:myString];


make any sense?
 
thanks for the responses, i took your advice and began reading the objective C tutorial in Documentation/. in this code, i think they described height: as being a label. so i know that this method is available to instances of its class(-), it is called setWidth, its arguments are a float number width and float height. is the word height: preceeding (float)height; only there for descriptive purposes?
Code:
- (void)setWidth:(float)width height:(float)height;
in other words, it could be written w/o the label as:
Code:
- (void)setWidth:(float)width :(float)height;
thanks again
 
I don't think it's the same signature if you remove the label (or even a valid signature). The second "height" is the one that doesn't matter so much, though something has to be there. So you could write
Code:
- (void)setWidth:(float)width height:(float)foo;
or
Code:
- (void)setWidth:(float)width height:(float)supercalifragilistic;
as long as the general definition is setWidth:height:.

-Rob
 
Back
Top