Last active
September 25, 2019 08:33
-
-
Save uranusjr/8995334 to your computer and use it in GitHub Desktop.
Sample implementation of an Objective-C class without Foundation framework.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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