Created
September 2, 2023 14:55
-
-
Save pookjw/3fbc2c93314dc1810b777b643c818b64 to your computer and use it in GitHub Desktop.
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 <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