Yes, check this piece of code, I created a class here using only C methods and one protocol definition (for simplicity when making calls) ...
Mycobject.h
#import <Foundation/Foundation.h> @protocol MyCObj_methods<NSObject> -(NSString *) getString; -(int) getInt; +(NSString *) someStaticMethod; @end typedef id<MyCObj_methods> MyCObj; extern Class MyCObj_class; __attribute__((constructor)) void MyCObj_initialize(); MyCObj MyCObj_alloc( id self, SEL _cmd ); MyCObj MyCObj_new( id self, SEL _cmd ); NSString *MyCObj_someStaticMethod ( id self, SEL _cmd ); MyCObj MyCObj_init( MyCObj self, SEL _cmd ); NSString *MyCObj_getString( MyCObj self, SEL _cmd ); int MyCObj_getInt( MyCObj self, SEL _cmd );
MyCObject.m
#import "MyCObject.h" #import <objc/Object.h> #import <objc/runtime.h> static Class myStaticClass; Class MyCObj_class; typedef struct { Class isa; NSString *myString; int myInt; } MyCObj_t; void MyCObj_initialize(void); __attribute__((constructor)) void MyCObj_initialize() { MyCObj_class = objc_allocateClassPair([NSObject class], "MyCObj", 0); objc_registerClassPair(MyCObj_class); myStaticClass = object_getClass(MyCObj_class); class_addMethod(MyCObj_class, @selector(init), (IMP)MyCObj_init, "@@:"); class_addMethod(MyCObj_class, @selector(getString), (IMP)MyCObj_getString, "@@:"); class_addMethod(MyCObj_class, @selector(getInt), (IMP)MyCObj_getInt, " i@ :"); class_addMethod(myStaticClass, @selector(alloc), (IMP)MyCObj_alloc, "@@:"); class_addMethod(myStaticClass, @selector(new), (IMP)MyCObj_new, "@@:"); class_addMethod(myStaticClass, @selector(someStaticMethod), (IMP)MyCObj_someStaticMethod, "@@:"); } MyCObj MyCObj_alloc(id self, SEL _cmd) { return (MyCObj) class_createInstance(MyCObj_class, sizeof(MyCObj_t) - sizeof(Class)); } MyCObj MyCObj_new(id self, SEL _cmd) { return (MyCObj) [[MyCObj_class alloc] init]; } NSString *MyCObj_someStaticMethod(id self, SEL _cmd) { return @"Some Static Method"; } MyCObj MyCObj_init(MyCObj self, SEL _cmd) { struct objc_super super = { .receiver = self, .super_class = [NSObject class] }; if ((self = (MyCObj) objc_msgSendSuper(&super, _cmd))) { ((MyCObj_t *) self)->myString = @"hello world!"; ((MyCObj_t *) self)->myInt = 15; } return self; } NSString *MyCObj_getString(MyCObj self, SEL _cmd) { return ((MyCObj_t *) self)->myString; } int MyCObj_getInt(MyCObj self, SEL _cmd) { return ((MyCObj_t *) self)->myInt; }
Using:
MyCObj obj = [MyCObj_class new]; NSLog(@"%@ %i %@", [obj getString], [obj getInt], [MyCObj_class someStaticMethod]);