| [ Return to Articles | Show Comments | Submit Comment ]
Article #1347: Manolo's Objective C "crash course"
Created at 13:22 Feb 06, 2014 by greg.ercolano
Manolo posted the following on fltk.general back on Dec 10, 2009:
___________________________________________________________________________________________________________
When you write in C++:
object->memberfunction(arg1, arg2, arg3);
..in objective-c this would be:
[object memberfunction:arg1 name-of-arg2:arg2 name-of-arg3:arg3];
..and this is called "sending the memberfunction:name-of-arg2:name-of-arg3: message to object".
This is declared by a prototype written as:
@interface class-name : parent-class-name {
class local variables;
}
- (return-type)memberfunction:(arg1-type)arg1-dummy name-of-arg2:(arg2-type)arg2-dummy name-of-arg3:(arg3-type)arg3-dummy;
@end
..and this stuff can, as in C++, return a value or not return anything.
All MAC OS defined class names begin by NS (which stands for "NextStep", Apple's older Unix-y OS),
and the subclasses I have defined by FL.
There is also the syntax:
[classname memberfunction:arg1 name-of-arg2:arg2]
..that corresponds to a static member function:
classname::memberfunction(arg1, arg2)
..and that usually allocates and returns an instance of this class (i.e., a class *).
One tends to nest things:
[[NSEvent alloc] initWithData:data]
..reads, in C++:
NSEvent *retval = NSEvent::alloc();
retval->initWithData(data);
With this, you can read most of objective-c !
A tricky thing, though, is memory allocation.
___________________________________________________________________________________________________________
Erco adds the following.. not an Objective C programmer, just picking it up from Wikipedia and google,
so there may be errors here (if so, let me know in the comments, and I'll edit the OP):
___________________________________________________________________________________________________________
//
// EXAMPLE CLASS DEFINITION AND PROTOTYPES
//
@interface classname : superclassname { // C++: class classname : public superclassname { ..
// INSTANCE VARIABLES GO HERE
}
// THESE ARE "CLASS METHOD" PROTOTYPES (as denoted by '+')
+ classMethod1; // C++: static void* classMethod1();
+ (int)classMethod2; // C++: static int classMethod2();
+ (int)classMethod3:(float)arg1; // C++: static int classMethod3(float arg1);
//
// THESE ARE "INSTANCE METHOD" PROTOTYPES (as denoted by '-')
- (int)instanceMethod1:(float)arg1; // C++: int instanceMethod1(float arg1);
- (int)instanceMethod2:(long)arg1 arg2_callName:(char)arg2; // C++: int instanceMethod2(long arg1, char arg2)
@end
//
// EXAMPLE CLASS IMPLEMENTATION
//
@implementation classname
- (int)instanceMethod1:(float)arg1
{
/* implementation of the method */
}
- (int)instanceMethod2:(long)arg1 arg2_callName:(char)arg2
{
/* implementation of the method */
}
@end
CAVEATS
=======
AFAICT, there is no equivalent of default arguments for functions/methods in Objective C.
In other words, the C++ prototype:
int someMethod(int a, int b, float gamma=2.0);
..has no equivalent in Objective C (since C doesn't support default args, neither does Objective C).
However, this can be emulated by defining two methods, one that takes the two arguments, and a second
that takes one, calling the first with the 'default' value, e.g.
- (int)someMethod:(int)a b:(int)b gamma:(float)gamma { .. }
- (int)someMethod:(int)a b:(int)b { [self someMethod:a b:b gamma:2.0]; }
Filename extensions:
.m -- Objective C (no C++ syntax allowed)
.mm -- Objective C++ (allows a mix of C++ and Objective C, which is what we use in FLTK)
[ Listing ]
[ Submit Comment ] |
| |