2

What is preferable to use here and why?

__weak typeof(self) weakSelf = self;

or

__weak MyObject *weakSelf = self;

Obviously, __weak id weakSelf = self; would be the least desirable, as we would not get type checking, is that correct?

However, between the first two... which is desirable and why?

Also, any reason to use __typeof instead of typeof, if clang supports using typeof?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
chris P
  • 6,359
  • 11
  • 40
  • 84
  • It's not an opinon-based question. These two statements are handled by the compiler in a different way so both usages have their pros and cons. – Michał Ciuba Apr 01 '16 at 20:14
  • @MichałCiuba They have pros and cons but there is no definitive answer. Different people will read pros and cons and choose differently. Therefore it's opinion based. – Sulthan Apr 01 '16 at 20:28

3 Answers3

3

I've recently starting writing it like this:

__typeof(self) __weak weakSelf = self;

Using the __typeof version lets you use a code snippet so it's way faster, and will also be consistent throughout your code.

This isn't the common form, as most people put the __weak variable qualifier at the beginning, but according to the docs here: https://developer.apple.com/library/mac/releasenotes/ObjectiveC/RN-TransitioningToARC/Introduction/Introduction.html the variable qualifier goes after the type.

Once inside the block, I use:

__typeof(weakSelf) __strong strongSelf = weakSelf;
if (!strongSelf) return;

to obtain a strong reference again.

Both of those are code snippets so I don't actually do much typing, or have to remember the exact syntax.

Dave Wood
  • 13,143
  • 2
  • 59
  • 67
  • can you elaborate on the code snippet? Are these little macros you can create within the xcode IDE? – chris P Apr 01 '16 at 20:23
  • Sure, Xcode has some code snippet support included, but I prefer to use Dash https://itunes.apple.com/us/app/dash-3-api-docs-snippets/id449589707?mt=12&uo=4&at=11lMGu&pt=17255&ct=SO since I also use it for docs. It lets you set up a keyword that when you type it, it's automatically replaced by the snippet. So I use `weakSelf#` and `strongSelf#` for those two. That's all I ever have to type in and I get all the complicated syntax consistently done for me. I also have snippets for setting up common blocks etc since it's a pain to remember the syntax for those. – Dave Wood Apr 01 '16 at 20:31
  • thanks. Also, is there a need to define __strong once in the block. That would be the default correct? Or do you place that there more for symmetry? – chris P Apr 01 '16 at 20:37
  • I believe you're correct that you don't need the `__strong` qualifier since it is the default, but I use it to increase the readability. It makes it explicitly clear to anyone else reading the code why we're creating another variable. – Dave Wood Apr 01 '16 at 20:41
  • I'm going mad trying to drag one line of code into snippets to create a snippet. Is there a menu option or keyboard shortcut? I literally cannot grab one line of text and drag it over. Tried 50 times. – chris P Apr 01 '16 at 20:48
  • I haven't used Xcode's snippets, so no idea how you'd add more. In Dash, it's easy, just hit the + and type in what you want. TextExpander https://smilesoftware.com/TextExpander is another common tool for this. – Dave Wood Apr 01 '16 at 20:56
1

The first is more convenient, because the code is the same regardless of class. You can use an Xcode code snippet to insert the typeof-using line of code with fewer keystrokes. You also have less work to do if you change the name of your class.

I don't know of any downside to using the typeof-version except maybe that it's a little harder to understand.

If you use __typeof__ instead of typeof, your code will compile regardless of the compiler's dialect setting.

Community
  • 1
  • 1
rob mayoff
  • 375,296
  • 67
  • 796
  • 848
-3

As you probably know __typeof__() "expands" to the concrete type of the argument. If the argument is self and the concrete type of self is MyObject (that is your case), it is simply the same.

So the first form has simply another indirection with the usual advantage and disadvantage of indirections:

  • The indirected form is more flexibel, because the indirection follows changes.

  • The indirected form is less readable, because the the reader has to understand the indirection in his mind.

Personally I would prefer the second form. But more important: Personally I would prefer to waste my time for more important questions. ;-)

Would you write:

__typeof__(self) copy = [self copy];

I wouldn't.

Amin Negm-Awad
  • 16,582
  • 3
  • 35
  • 50