Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Syntax help

Syntax help
Thread Tools
Fresh-Faced Recruit
Join Date: Oct 2003
Location: Nebraska
Status: Offline
Reply With Quote
Oct 16, 2003, 03:05 AM
 
I am a javascript and VB programmer and have been starting to learn C and Cocoa just recently so please forgive me if this seems to be a stupid question. I thought that I was getting a pretty good handle on C syntax until I started reading some Cocoa tutorials. Now I'm stumped. The problem is code such as the following

[data writeToFile:[sheet filename] atomically:YES];

I am familiar with using square brackets for arrays but not the way they are used here, as well as the use of the colon toward the end of the line. Also this one

- (void)setImageNSImage *)newImage;

A type in parenthises at the beginning of the line? I don't want to just cut and paste things like this but for obvious reasons would like to understand what the code is doing. If someone patient could help that would be greatly appreciated.
     
Mac Elite
Join Date: Sep 2000
Location: Tempe, AZ
Status: Offline
Reply With Quote
Oct 16, 2003, 04:49 AM
 
In objective-C, the concept of "function" is replaced with the concept of sending a message (identified by a "selector") to either a class object or to some object that is an instance of some class. When you define a "selector" that instantiated objects can respond to, the definition looks like this:
Code:
- (someReturnType)nameOfSelector: (arg1Type)arg1 nameContinued: (arg2Type)arg2 nameContinued: (arg3Type)arg3;
A specific example (taken from NSView) is
Code:
- (void)drawRect: (NSRect)theRect;
The selector is named "drawRect:". The trailing colon in the name indicates that an argument precedes the name. When you call that selector, there is no return value (void), and you call the selector using a single NSRect.

Another example (taken from NSColor) is
Code:
+ (NSColor *)colorWithCalibratedRed: (float)red green: (float)green blue: (float)blue alpha: (float)alpha;
Note the leading "+". This indicates that this selector represents a message that must be sent to the class object, not to a specific instantiated object belonging to that class. The "-" preceeding drawRect: meant that the drawRect: selector must be sent only to instantiated objects.

The selector name is "colorWithCalibratedRed:green:blue:alpha:" and when you send this message to the NSColor class, an instantiated NSColor object is returned, as in:
Code:
NSColor *myColor = [NSColor colorWithCalibratedRed: 0.5 green: 0.7 blue: 1.0 alpha: 1.0];
Which leads to your question about square brackets. They're how you send messages. The object you're sending to is the first item inside of the brackets (in this case, the singleton NSColor class object). The object is followed by the name of the selector interwoven with it's arguments. In the example above, when the message is received by the NSColor class object, it will be with red=0.5, green=0.7, blue=1.0 and alpha=1.0.

Likewise, to send the drawRect: message shown above, you could do something like
Code:
[myViewObject drawRect: someRectangle];
Since drawRect: must be sent to instantiated objects (the leading "-" in the selector definition), the object the message is sent to is something that's been instatiated (myViewObject). someRectangle is the NSRect that will be passed to theRect when the message is received.

I suspect I've probably confused you more, but this is actually pretty easy (and very very nice!!!) once you get accustomed to it. I've tried to use rigorous language, which has probably made things more confusing.

Anyway, for more info, check out Apple's docs on the Objective-C language. Good luck!
Geekspiff - generating spiffdiddlee software since before you began paying attention.
     
Senior User
Join Date: Feb 2001
Location: Deer Crossing, CT
Status: Offline
Reply With Quote
Oct 16, 2003, 07:46 AM
 
Hammer65,

I'll try to relate Obj-C messages to VB (pre .NET) code.

In VB, to set the text of a text control, you would code something like the following (my VB is rusty, if I'm not 100% here don't shoot, OK?):

text1.setText "my text"

In Obj-C, you send a "message" to an object, so the same process above might look something like:

[text1 setText:@"my text"];

The brackets surround a message to an object. In Obj-C, spaces are used instead of '.' to separate objects and methods. The colon after setText separates a method from the argument being sent to that method. If there are multiple arguments, they have to be identified by name.

The '@' symbol says to treat the text in quotes as an NSString object (which was part of the Obj-C additions, hence the '@' to differentiate an NSString from a char array).

If I'm not 100% here (I just started learning Obj-C using the Cocoa for Dummies book last week) someone please correct me.
     
Senior User
Join Date: Nov 2001
Location: State of Denial
Status: Offline
Reply With Quote
Oct 16, 2003, 10:26 AM
 
I'd grab a book of some kind, really.
[Wevah setPostCount:[Wevah postCount] + 1];
     
Fresh-Faced Recruit
Join Date: Oct 2003
Location: Nebraska
Status: Offline
Reply With Quote
Oct 17, 2003, 12:32 AM
 
Thank all of you for your responses. It does make sense now. The trouble was that in my haste, i was scouring C docs looking for what I thought might be a shortcut of some sort that experienced programmers used but that wasn't in most tutorials. Interesting that none of the Cocoa tutorials explained this either.

It's actually a very elegant way to do things. I think I'm going to like Objective C. Thanks again.
     
Senior User
Join Date: Feb 2001
Location: Deer Crossing, CT
Status: Offline
Reply With Quote
Oct 17, 2003, 08:33 AM
 
There is a .pdf file somewhere in the Developer tree that explains Objective-C in totality. I forget the name, but it's definitely somewhere in /Developer/Documentation . . .

This will have you up and running in Obj-C.

I was digging through the documentation folder and there are multiple full books buried in there!
     
Mac Elite
Join Date: Sep 2000
Location: Tempe, AZ
Status: Offline
Reply With Quote
Oct 17, 2003, 08:36 AM
 
Originally posted by PBG4 User:
There is a .pdf file somewhere in the Developer tree that explains Objective-C in totality. I forget the name, but it's definitely somewhere in /Developer/Documentation . . .

This will have you up and running in Obj-C.

I was digging through the documentation folder and there are multiple full books buried in there!
The link I gave above leads to the HTML version of this.
Geekspiff - generating spiffdiddlee software since before you began paying attention.
     
Mac Elite
Join Date: Aug 2001
Status: Offline
Reply With Quote
Oct 17, 2003, 10:26 AM
 
Originally posted by Hammer65:
I am a javascript and VB programmer and have been starting to learn C and Cocoa just recently so please forgive me if this seems to be a stupid question. I thought that I was getting a pretty good handle on C syntax until I started reading some Cocoa tutorials. Now I'm stumped. The problem is code such as the following

[data writeToFile:[sheet filename] atomically:YES];

I am familiar with using square brackets for arrays but not the way they are used here, as well as the use of the colon toward the end of the line. Also this one

- (void)setImageNSImage *)newImage;

A type in parenthises at the beginning of the line? I don't want to just cut and paste things like this but for obvious reasons would like to understand what the code is doing. If someone patient could help that would be greatly appreciated.
Basically the problem you're running into is that you're learning C, and Cocoa doesn't use C, it uses Objective-C. Everyone else's comments explain what that is well enough.
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 07:16 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2