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:
The format of calls to functions outside of a class would just be:
If this was a class method it would be:
If it was an instance method, it would be:
If we assume a C function that operates on something called plotter, that's an int, this would look like:
myFunctions.h:
myFunctions.m:
Elsewhere in the code:
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:
Then in drvrchk.m, you would have:
Then in another file:
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
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