Skip to content

Instantly share code, notes, and snippets.

@pookjw
Created September 2, 2023 14:55
Show Gist options
  • Save pookjw/3fbc2c93314dc1810b777b643c818b64 to your computer and use it in GitHub Desktop.
Save pookjw/3fbc2c93314dc1810b777b643c818b64 to your computer and use it in GitHub Desktop.
#import <Foundation/Foundation.h>
@interface MyPlaceholderValue : NSObject
- (instancetype)initWithInteger:(NSInteger)number;
@end
@implementation MyPlaceholderValue
- (id)initWithInteger:(NSInteger)number {
CFNumberRef result = CFNumberCreate(NULL, kCFNumberNSIntegerType, &number);
return (id)result;
}
@end
@interface MyNumber : NSObject
- (instancetype)initWithInteger:(NSInteger)number;
@end
@implementation MyNumber
+ (instancetype)allocWithZone:(struct _NSZone *)zone {
static id _Nullable __placeholderNumber = nil;
if (!__placeholderNumber) {
__placeholderNumber = NSAllocateObject(MyPlaceholderValue.class, 0, zone);
}
return __placeholderNumber;
}
@end
int main(int argc, const char * argv[]) {
MyNumber *number = [[MyNumber alloc] initWithInteger:3];
NSLog(@"%@", number); // 3
[number release];
NSLog(@"%@", number); // 3
int count = 0;
while (count++ < 100) {
[number release];
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment