int main() { Class myClass = objc_getClass("Object"); id nilClass = class_createInstance(Nil, 0); id testOne = class_createInstance (myClass, 0); /* testassert(obj->isa == [Fake class]); testassert(object_setClass(obj, [Super class]) == [Fake class]); testassert(obj->isa == [Super class]); testassert(object_setClass(nil, [Super class]) == nil); testassert(object_getClass(obj) == buf[0]); testassert(object_getClass([Super class]) == [Super class]->isa); testassert(object_getClass(nil) == Nil); testassert(0 == strcmp(object_getClassName(obj), "Super")); testassert(0 == strcmp(object_getClassName([Super class]), "Super")); testassert(0 == strcmp(object_getClassName(nil), "nil")); testassert(0 == strcmp(class_getName([Super class]), "Super")); testassert(0 == strcmp(class_getName([Super class]->isa), "Super")); testassert(0 == strcmp(class_getName(nil), "nil")); succeed(__FILE__); */ fprintf (stderr, "Done\n"); }
void applicationdidFinishLaunching(void *receiver, struct objc_selector *selector, void *application) { Class windowClass = (Class) objc_getClass("UIWindow"); void * windowInstance = class_createInstance(windowClass, 0); objc_msgSend(windowInstance, sel_registerName("initWithFrame:"),(Rect){0,0,320,480}); //Make Key and Visiable objc_msgSend(windowInstance, sel_registerName("makeKeyAndVisible")); //Create Table View Class TableViewController = (Class) objc_getClass("UITableViewController"); void *tableViewController = class_createInstance(TableViewController, 0); objc_msgSend(tableViewController, sel_registerName("init")); void *tableView = objc_msgSend(tableViewController, sel_registerName("tableView")); objc_msgSend(tableView, sel_registerName("setDataSource:"),createDataSource()); objc_msgSend(tableView, sel_registerName("setDelegate:"),createDelegate()); Class NavController = (Class) objc_getClass("UINavigationController"); navController = class_createInstance(NavController, 0); objc_msgSend(navController, sel_registerName("initWithRootViewController:"),tableViewController); void *view = objc_msgSend(navController, sel_registerName("view")); //Add Table View To Window objc_msgSend(windowInstance, sel_registerName("addSubview:"),view); }
void * createDelegate() { Class superclass = (Class) objc_getClass("NSObject"); Class DataSource = objc_allocateClassPair(superclass, "Delegate", 0); class_addMethod(DataSource, sel_registerName("tableView:didSelectRowAtIndexPath:"), (void(*))tableView_didSelectRowAtIndexPath, nil); objc_registerClassPair(DataSource); return class_createInstance(DataSource, 0); }
id object_copy(id obj, size_t size) { Class cls = object_getClass(obj); id cpy = class_createInstance(cls, size - class_getInstanceSize(cls)); memcpy(((char*)cpy + sizeof(id)), ((char*)obj + sizeof(id)), size - sizeof(id)); return cpy; }
Protocol *objc_allocateProtocol(const char *name) { if (objc_getProtocol(name) != NULL) { return NULL; } Protocol *p = (Protocol*)class_createInstance((Class)incompleteProtocolClass(), 0); p->name = strdup(name); return p; }
void *createDataSource() { Class superclass = (Class) objc_getClass("NSObject"); Class DataSource = objc_allocateClassPair(superclass, "DataSource", 0); class_addMethod(DataSource, sel_registerName("tableView:numberOfRowsInSection:"), (void(*))tableView_numberOfRowsInSection, nil); class_addMethod(DataSource, sel_registerName("tableView:cellForRowAtIndexPath:"), (void(*))tableView_cellForRowAtIndexPath, nil); objc_registerClassPair(DataSource); return class_createInstance(DataSource, 0); }
void tableView_didSelectRowAtIndexPath(void *receiver, struct objc_selector *selector, void *tblview, void *indexPath) { Class ViewController = (Class) objc_getClass("UIViewController"); void * vc = class_createInstance(ViewController, 0); objc_msgSend(vc, sel_registerName("init")); char buffer[8]; int row = (int) objc_msgSend(indexPath, sel_registerName("row")); sprintf (buffer, "Item %d", row); void *label = objc_msgSend(objc_getClass("NSString"), sel_registerName("stringWithUTF8String:"),buffer); objc_msgSend(vc, sel_registerName("setTitle:"),label); objc_msgSend(navController, sel_registerName("pushViewController:animated:"),vc,1); }
void *tableView_cellForRowAtIndexPath(void *receiver, struct objc_selector *selector, void *tblview, void *indexPath) { Class TableViewCell = (Class) objc_getClass("UITableViewCell"); void *cell = class_createInstance(TableViewCell, 0); objc_msgSend(cell, sel_registerName("init")); char buffer[7]; int row = (int) objc_msgSend(indexPath, sel_registerName("row")); sprintf (buffer, "Row %d", row); void *label = objc_msgSend(objc_getClass("NSString"), sel_registerName("stringWithUTF8String:"),buffer); objc_msgSend(cell, sel_registerName("setText:"),label); return cell; }
id objc_allocate_object(Class cls, int extra) { return class_createInstance(cls, extra); }