We all know that when you prevent a circular reference such as a block, the __weak keyword is used to define the following:
__weak typeof(selfself;
Later, in order to facilitate, not every time to write such a fixed code, we define a macro:
#defineWeakSelf __weak typeof(selfself;
After that, we can be more convenient where needed:
WeakSelf;...[weakSelf doSomething];
Later, we found that not only self needs to use weak, some variables may also need to be weak, so our macro continues to evolve, not only to support self:
#define WeakObj(o) __weak typeof(o) o##Weak = o;
In this way, subsequent to the need to use weak object, just write a sentence WeakObj(obj) can use the Objweak variable (PS: found no, the variable name generated here is actually objweak, not weakobj, for reasons see note 1 at the end of the article)
Later on, we found some tips to make this macro look more native, and we added the @ symbol in front:
#define WeakObj(o) autoreleasepool{} __weak typeof(o) o##Weak = o;
It looks like this on the use
@WeakObj(obj);...[selfWeak doSomething];
Does it feel pretty big on the top?
Here is the use of @autoreleasepool{} This system keyword to achieve, in fact, you can also use the @try{} @finally {} This can also achieve the same effect, such as:
#defineWeakObj(otry{}@finally{} __weak typeof(o) o##Weak = o;
This partially empty @try or empty @autoreleasepool will be optimized at compile time without worrying about performance issues.
At this point, our macro has been able to use, but the actual use, there has been a very embarrassing problem, is the code auto-completion, @w does not automatically prompt out the macro, so every time is very embarrassing to use the hint, write Weakobj (obj), and then move the cursor to the front to hit the last @ symbol.
How can this kind of thing endure?
Fortunately we also have the tool, Xcode CodeSnippet, any method within, write a code
@WeakObj(<#obj#>);
Drag to the CodeSnippet area of Xcode and set the shortcut key to @weakobj.
At this point, happy to use @w can automatically fill out the macro.
In addition, there is the corresponding strong macro, together with the sun here
#define StrongObj(o) autoreleasepool{} __strong typeof(o) o = o##Weak;
It's useful to write a simple example:
@WeakObj(selfsetBlock:^{ @StrongObj(self); [self doSomething];}];
For this reason, please try to figure it out, then you can look at 晓月 this article: http://blog.csdn.net/uxyheaven/article/details/44226395, you can also leave a comment in the comments.
Finally, we reveal why the variable name generated by the macro is Objweak:
1. When using, if the developer is accustomed to play [self dosomething], when he input, automatically fill out the parts can see there is selfweak to choose, is a kind of reminder.
2. If the weak front, of course, you can generate a variable name such as Weakobj, only need to o##Weak replace weak##o the macro
Well, this article wants to inspire or help some people.
The last person to bask in the macro definition:
#define YRWeakObj(o) autoreleasepool{} __weak typeof(o) o##Weak = o;#define YRStrongObj(o) autoreleasepool{} __strong typeof(o) o = o##Weak;
Copyright NOTICE: This article is for bloggers original article, welcome reprint but please keep the article source. http://blog.csdn.net/u010124617
iOS Development tips for--weakself macro evolution