Skip to content

Instantly share code, notes, and snippets.

@McZonk
Created September 21, 2014 21:07
Show Gist options
  • Save McZonk/52ba3618fa3372f134f8 to your computer and use it in GitHub Desktop.
Save McZonk/52ba3618fa3372f134f8 to your computer and use it in GitHub Desktop.
#ifdef __OBJC__
// clang -framework Foundation -o ObjC -ObjC main.c
#import <Foundation/Foundation.h>
int main(int argc, const char **argv)
{
NSString *string = [[NSString alloc] initWithUTF8String:"Objective-C"];
const char *cstring = [string UTF8String];
printf("Hello %s\n", cstring);
return 0;
}
#else
// clang -framework Foundation -o C main.c
#include <objc/runtime.h>
#include <objc/message.h>
#include <stdio.h>
extern void objc_release(id object);
int main(int argc, const char **argv)
{
Class NSStringClass = objc_getClass("NSString");
SEL allocSelector = sel_getUid("alloc");
SEL initWithUTF8StringSelector = sel_getUid("initWithUTF8String:");
SEL UTF8StringSelector = sel_getUid("UTF8String");
id string = objc_msgSend(objc_msgSend((id)NSStringClass, allocSelector), initWithUTF8StringSelector, "Objective-C runtime only");
const char *cstring = (const char *)objc_msgSend(string, UTF8StringSelector);
printf("Hello %s\n", cstring);
objc_release(string);
return 0;
}
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment