Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @import Foundation;
- @import ObjectiveC.runtime;
- @interface Foo: NSObject
- - (instancetype)initWithTag:(NSString*)tag;
- @property(nonatomic, readonly, copy) NSString* tag;
- + (instancetype)foo_1;
- + (instancetype)foo_2;
- + (instancetype)foo_3;
- @end
- @implementation Foo
- - (instancetype)initWithTag:(NSString*)tag {
- if ((self = [super init])) {
- _tag = [tag copy];
- }
- return self;
- }
- + (BOOL)resolveClassMethod:(SEL)sel {
- Foo *methodRetVal = [[self alloc] initWithTag:NSStringFromSelector(sel)];
- Foo * (^block)(id blockSelf) = ^Foo *(id blockSelf) {
- return methodRetVal; // <- Read happens here, when heap block is invoked and reads captured variable
- };
- IMP imp = imp_implementationWithBlock(block); // <- Write happens here, when captured variable is copyied from stack block to heap block
- class_addMethod(object_getClass(self), sel, imp, "@@:");
- return YES;
- }
- @end
- int main(int argc, char * argv[]) {
- @autoreleasepool {
- for (int i = 0; i < 100; ++i) {
- dispatch_async(dispatch_get_global_queue(QOS_CLASS_DEFAULT, 0), ^{
- NSLog(@"%@", [Foo foo_1].tag);
- });
- }
- return 0;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement