Skip to content

Instantly share code, notes, and snippets.

@uranusjr
Last active September 25, 2019 08:33
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save uranusjr/8995334 to your computer and use it in GitHub Desktop.
Save uranusjr/8995334 to your computer and use it in GitHub Desktop.
Sample implementation of an Objective-C class without Foundation framework.
#import <stdio.h>
#import <stdlib.h>
#import <objc/runtime.h>
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wobjc-root-class"
#pragma clang diagnostic ignored "-Wdeprecated-objc-isa-usage"
@interface Answer
{
Class isa;
}
+ (id)instantiate;
- (void)die;
@property(assign, nonatomic) int value;
@end
@implementation Answer
+ (id)instantiate
{
Answer *result = malloc(class_getInstanceSize(self));
result->isa = self;
return result;
}
- (void)die
{
free(self);
}
@end
#pragma clang diagnostic pop
int main(int argc, char const *argv[])
{
Answer *answer = [Answer instantiate];
answer.value = 42;
printf("The answer is: %d\n", answer.value);
[answer die];
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment