Skip to content
This repository has been archived by the owner on Dec 5, 2019. It is now read-only.

How to weakify super access? #119

Open
k06a opened this issue May 31, 2016 · 3 comments
Open

How to weakify super access? #119

k06a opened this issue May 31, 2016 · 3 comments

Comments

@k06a
Copy link
Contributor

k06a commented May 31, 2016

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?

@jspahrsummers
Copy link
Owner

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];
}

@kastiglione
Copy link
Collaborator

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.

@k06a
Copy link
Contributor Author

k06a commented Jun 1, 2016

@kastiglione heh, that looks really horrible!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants