Thursday, November 9, 2006 11:05 - Code
yes, it's possible because ObjectiveC is a strict superset of C. You just need to use a *.m extension so that Xcode compiles it as ObjectiveC and not just C.
I already used C++ to write maxmsp externals so I knew it should be possible with another C based language. But I needed a proof of concept. here it is:
#include "ext.h"
#import <Foundation/NSObject.h>
@interface Fraction : NSObject
{
int num;
int den;
}
- (void) setNumerator: (int) d;
- (void) setDenominator: (int) d;
- (int) numerator;
- (int) denominator;
- (void) print;
@end
@implementation Fraction
- (void) setNumerator: (int) d
{
num = d;
}
- (void) setDenominator: (int) d
{
den = d;
}
- (int) numerator
{
return num;
}
- (int) denominator
{
return den;
}
- (void) print
{
post("%i/%i",num,den);
}
@end
typedef struct
{
t_object o;
Fraction *frac; // to show how we can store an ObjectiveC instance inside a C struct.
} objc_t;
static t_messlist *objc_class = NULL;
static void *objc_new(Symbol *s, short ac, Atom *at)
{
objc_t *o = (objc_t *)newobject(objc_class);
Fraction *frac = [[Fraction alloc] init];
[frac setNumerator: 1];
[frac setDenominator: 4];
[frac print];
o->frac = frac;
return o;
}
static void objc_free(objc_t *o)
{
Fraction *frac = o->frac;
if(frac)
{
[frac release];
}
}
int main(void)
{
setup(&objc_class,
(method)objc_new,
(method)objc_free,
(short)sizeof(objc_t), 0L,
A_GIMME, 0);
}
| permalink
| related link
| ( 3 / 6099 )