libextobjc icon indicating copy to clipboard operation
libextobjc copied to clipboard

How to weakify super access?

Open k06a opened this issue 9 years ago • 3 comments

I don't wanna strong capture self in this block:

void(^block)() = ^{
    [super someMethod];
};

Looks like I need to do something like this:

@weakify(self);
void(^block)() = ^{
    @storngify(self);
    [self.super someMethod];
};

But looks like this will not works as I want. I have solved this issue with this trick:

@weakify(self);
void(^block)() = ^{
    @storngify(self);
    [self superSomeMethod];
};

- (void)superSomeMethod {
    [super someMethod];
}

Is there any way to achieve this without creating helper method?

k06a avatar May 31 '16 20:05 k06a

I don't know of any, unfortunately. A similar problem affects ivar access that isn't explicitly done through self:

@weakify(self);
dispatch_block_t block = ^{
  @strongify(self);

  // whoops, retain cycle
  [_someIvar doAThing];
}

jspahrsummers avatar Jun 01 '16 08:06 jspahrsummers

I think you could do:

@weakify(self);
dispatch_block_t block = ^{
  @strongify(self);
  objc_msgSendSuper(&(struct objc_super){ self, [self superclass] }, @selector(someMethod));
}

You'll need to cast objc_msgSendSuper to the appropriate function pointer type.

kastiglione avatar Jun 01 '16 17:06 kastiglione

@kastiglione heh, that looks really horrible!

k06a avatar Jun 01 '16 19:06 k06a