30

This is used in the weakify pattern of Objective-C

My guess is that it means: assign a weak reference to self with the name 'weakSelf' and the typeof self (e.g. MyViewController)

If it's correct and looks obvious to you: I want to be absolutely sure to get this right. Thanks.

brainray
  • 12,512
  • 11
  • 67
  • 116
  • @CrimsonChris Nice but one has to include `libextobjc/EXTScope`, it is not a part of LLDB. See: [weakify](http://aceontech.com/objc/ios/2014/01/10/weakify-a-more-elegant-solution-to-weakself.html) – zaph May 19 '14 at 16:33
  • 2
    Weakify and Strongify just wrap all that typeof nonsense and suppress some compiler warnings about shadowing self. It's all the same, but with a few more tricks to make it readable. – CodaFi May 19 '14 at 16:34
  • the link is dead – Richard Topchii Aug 12 '22 at 09:31
  • removed dead link – brainray Aug 16 '22 at 07:30

4 Answers4

29

My guess is that it means: assign a weak reference to self with the name weakSelf and the typeof self (e.g. MyViewController)

Yes, that is almost exactly what it means. The type of self would be MyViewController* (with an asterisk) not MyViewController.

The idea behind using this syntax instead of simply writing

MyViewController __weak *weakSelf = self;

is making it easier to refactor the code. The use of typeof also makes it possible to define a code snippet that can be pasted anywhere in your code.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
12

Using @weakify and @strongify from libExtObjC helps to simplify the "weak-strong dance" one has to do sometimes around blocks.

Example!

__weak __typeof(self) weakSelf = self;
__weak __typeof(delegate) weakDelegate = delegate;
__weak __typeof(field) weakField = field;
__weak __typeof(viewController) weakViewController = viewController;
[viewController respondToSelector:@selector(buttonPressed:) usingBlock:^(id receiver){
    __strong __typeof(weakSelf) strongSelf = weakSelf;
    __strong __typeof(weakDelegate) strongDelegate = weakDelegate;
    __strong __typeof(weakField) strongField = weakField;
    __strong __typeof(weakViewController) strongViewController = weakViewController;

versus...

@weakify(self, delegate, field, viewController);
[viewController respondToSelector:@selector(buttonPressed:) usingBlock:^(id receiver){
    @strongify(self, delegate, field, viewController);
brainray
  • 12,512
  • 11
  • 67
  • 116
CrimsonChris
  • 4,651
  • 2
  • 19
  • 30
  • 6
    I think you should mention that this answer requires 'libextobjc' – Logan May 19 '14 at 16:47
  • 1
    @CrimsonChris why do you weakify the delegate? I guess already was. – Ricardo Dec 07 '15 at 17:41
  • If I created the delegate like so... `MyDelegate *delegate = [[MyDelegate alloc] init];` I would have a strong reference to it until the current method ended. It wouldn't matter if the property on `viewController` was weak, I would still be creating a retain cycle if I didn't weakify it. – CrimsonChris Dec 07 '15 at 17:56
5

Your interpretation is correct. However, I find that when it is written that way, it is slightly confusing to read. I prefer it with an additional space after typeof(self):

__weak typeof(self) weakSelf = self;
Zev Eisenberg
  • 8,080
  • 5
  • 38
  • 82
4

Depending on your compiler settings, you may get a warning "Expected ';' after expression". You can fix this by changing it to use __typeof like this: __typeof(self) __weak weakSelf = self;

Thanks to Leo Natan and this question: https://stackoverflow.com/a/32145709/1758224.

Community
  • 1
  • 1
Eli Burke
  • 2,729
  • 27
  • 25