Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

lee1210

macrumors 68040
Original poster
Jan 10, 2005
3,182
3
Dallas, TX
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:
Code:
void function_name(int, int);

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

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

If it was an instance method, it would be:
Code:
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:
Code:
void dvrchk(int);

myFunctions.m:
Code:
#import "myFunctions.h"

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

Elsewhere in the code:
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:

Code:
#import <Cocoa/Cocoa.h>

@interface dvrchk : NSObject
{
  int memberA;
}

- (void) plotter;
@end

Then in drvrchk.m, you would have:
Code:
#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:
Code:
#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

macrumors 6502a
Dec 4, 2006
748
0
Katy, Texas
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
 

lee1210

macrumors 68040
Original poster
Jan 10, 2005
3,182
3
Dallas, TX
@ 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. [BOLD]Mac Player[/BOLD] already stated that it is used to distinguish this sort of string literal from a char * string literal in C.

-Lee
 

lee1210

macrumors 68040
Original poster
Jan 10, 2005
3,182
3
Dallas, TX
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/document...ml#//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

macrumors 68040
Oct 25, 2004
3,539
272
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).
 

Eraserhead

macrumors G4
Nov 3, 2005
10,434
12,250
UK
This has now been typed up as a Mac Guide:

[guide]Objective-C Tutorial[/guide]

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

macrumors 65816
Feb 28, 2008
1,120
2
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...
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.