PDA

View Full Version : Mini Objective-C tutorial




lee1210
Jun 27, 2008, 12:33 AM
All,

I received a PM about how to write a new instance method (at least I think). I wrote quite a bit, and figured I'd put something up here. I'll try to edit it so it's a little more broad than my original response. Feel free to make corrections to the style, content, etc. I am pretty familiar with Objective-C from an academic perspective, but do not use it regularly. I used perl as a point of reference as the person asking was familiar.


Subroutines are generally called methods in the Objective-C world. They are divided into class methods and instance methods. A class method is noted with a plus in declarations and documentation. An instance method is marked with a hyphen/minus sign. Perl doesn't make you decide this, every method can be called on a class or an instance of the class.

Generally things like allocWithX methods that return a new instance of a class (essentially a constructor) are class methods, and things like getters and setters for properties/member variables are instance methods.

The term thread in the Objective-C world is the same as in every other language, including the Thread module in perl. There is an NSThread class for high level management of threads which can be used as an alternative to C-style POSIX threads. (This was an aside...)

There are also regular, C-style functions that are declared like anything else in C. In this case there is no - or + associated, and the prototype in your header file is in the form:
void function_name(int, int);

The format of calls to functions outside of a class would just be:
dvrchk(plotter);

If this was a class method it would be:
[MyClass dvrchk:plotter];

If it was an instance method, it would be:
MyClass *MyInstance = [[MyClass alloc] init];
[MyInstance dvrchk:plotter];

If we assume a C function that operates on something called plotter, that's an int, this would look like:

myFunctions.h:
void dvrchk(int);

myFunctions.m:
#import "myFunctions.h"

void dvrchk(int plotter){
NSLog(@"Plotter is: %d\n",plotter);
}

Elsewhere in the code:
#import "myFunctions.h"
...
int myPlot = 5;
dvrchk(myPlot);
...

If you want to write a class called dvrchk (Note: this was the person who posed the questions name, I'd use camelcase per convention), and it will have a method called plotter. That's a totally different situation. For that you would have dvrchk.h, and it would have:

#import <Cocoa/Cocoa.h>

@interface dvrchk : NSObject
{
int memberA;
}

- (void) plotter;
@end

Then in drvrchk.m, you would have:
#import "dvrchk.m"

@implementation dvrchk

- (void) plotter{
NSLog(@"The current member value is: %d\n",memberA);
}

- init {
if(![super init]) return nil;
memberA = 7;
return self;
}
@end

Then in another file:

#import "dvrchk.m"
...
void myFunction(){
dvrchk *myDvrchk = [[dvrchk alloc] init];
[myDvrchk plotter];
}


This is, in essence, the contents of any Objective-C tutorial. For example, this one:
http://www.geom.uiuc.edu/software/w3kit/Overview/ObjectiveC.html

This is pretty basic, and as I said I am open to criticism as I'm certainly not a salty Obj-C pro. Hopefully the response was helpful to the person who PM'd any perhaps will be helpful to others starting out.

-Lee



toddburch
Jun 27, 2008, 07:29 AM
Thanks for taking the time Lee. I've not yet dove into Obj-C, but I plan on it. The most I got out of this was the - and + prefix characters delineating the instance or class methods.

I have a question. The @ character - it appears both in what appears to be a class definition (@interface) and also in front of a string in the NSLog function. Can you explain a bit more on @?

Thanks, Todd

Mac Player
Jun 27, 2008, 07:41 AM
The @ before a string distinguishes a C string from a NSString.

lee1210
Jun 27, 2008, 08:01 AM
@ is used a lot in the objective-C world. It is mostly used to avoid taking english words and making them reserved (for example, you can't have a variable called float in C/Objective-C because this is a reserved word). Here's the list of what I could find/think of that use it:
@implementation
@interface
@end
@synthesize
@dynamic
@public
@private
@property

These are used to define a class and its properties. Many are new in Objective-C 2.0, and are used to indicate the type of a property for getter and setter generation, etc.

There are also @"" NSString literals. It is essentially shorthand for NSString's +stringWithUTF8String method. Mac Player already stated that it is used to distinguish this sort of string literal from a char * string literal in C.

-Lee

lee1210
Jun 27, 2008, 09:00 AM
Double post, sorry. This really isn't an edit.

While it's not part of the language grammar, %@ is used as a format specifier frequently as well. Even though this is a library/framework thing and not part of the language proper it's definitely worth knowing. From this page:
http://developer.apple.com/documentation/Cocoa/Conceptual/Strings/Articles/formatSpecifiers.html

"Objective-C object, printed as the string returned by descriptionWithLocale: if available, or description otherwise"

This is necessary when printing NSString's themselves, or any other object. This can be called on anything that extends NSObject, as:
http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Protocols/NSObject_Protocol/Reference/NSObject.html#//apple_ref/occ/intfm/NSObject/description

this describes the protocol for description.

This is why you frequently see %@ in NSLog format strings, etc. I'm not sure what people's backgrounds are, but this is like toString in Java, which is automatically called when applying + to a String and Object, etc.

-Lee

iSee
Jun 27, 2008, 09:07 AM
I speculate that the creators of Objective-C used the @ symbol because it isn't valid in regular C at all (It is valid in a string constant, I suppose, but I can't think of anything else). So it is a straightforward way to distinguish Objective-C code from C code. It also makes it simpler to ensure that Objective-C does not redefine the meaning/behavior of valid C code.

Heh, when I look at my keyboard, I only see one other key on my keyboard that isn't valid in any context in C (that I can think of right now):
`
which is easily confused by a human reader with '

Using @ should make it easier to bolt an Objective-C compiler on to an existing C compiler. Because the @ isn't valid in any context in C except a string literal, the tokenizer (an early and simple step in the compiler) could be modified to simply look for the @ character outside of a string constant (the tokenizer understands string literals, so it is in a position to distinguish this). When @ is encountered the tokenizer would put the rest of the compiler in "Objective-C mode." (The Objective-C parser would be responsible for returning the compiler back to regular C mode when it detects the end of the Objective-C code).

toddburch
Jun 27, 2008, 10:12 AM
OK, thanks y'all. This Obj-C syntax is getting less cryptic every day for me!

Eraserhead
Jun 27, 2008, 10:55 AM
This has now been typed up as a Mac Guide:

Objective-C Tutorial

If further contributions could be typed up there that'd be good. I'm going to add the guide to the yellow bar at the top (along with the other developer resources).

glossywhite
Sep 1, 2009, 07:49 AM
Hi there. I think this tutorial would benefit from syntax highlighting, as it is pretty difficult to navigate code that is ALL one colour. Just an idea...