Objective-C Tutorial

From Mac Guides

Jump to: navigation, search
This article or section is based on a forum post written by lee1210.


Contents

Methods (Subroutines)

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.

Generally things like initWithX 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.

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];
}

General Form

This article or section is based on a forum post written by robbieduncan.


The general form for a method calling is the following

[<object> <method>:<firstvariable> <secondmethodpart>:<secondvariable>];

The @ symbol

@ 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 a partial list of commands 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. [BOLD]Mac Player[/BOLD] already stated that it is used to distinguish this sort of string literal from a char * string literal in C.

%@ Formatter

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. (As shown here) "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 shown here)

This is why you frequently see %@ in NSLog format strings, etc. this is like toString in Java, which is automatically called when applying + to a String and Object, etc.

Background on this usage

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).

General Tips

Threads

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.

External Links