bra*_*ray 26 typeof objective-c weak
这用于Objective-C 的弱化模式
我的猜测是它意味着:为自己分配一个名为'weakSelf'的自我弱引用和自我类型(例如MyViewController)
如果它是正确的并且看起来很明显:我希望绝对确保做到这一点.谢谢.
das*_*ght 27
我的猜测是,这意味着:用自己的名字
weakSelf和typeof自我分配一个弱的引用(例如MyViewController)
是的,这几乎就是它的含义.的类型的self将是MyViewController*(带星号)不MyViewController.
使用这种语法而不是简单地写的背后的想法
MyViewController __weak *weakSelf = self;
Run Code Online (Sandbox Code Playgroud)
使重构更容易.通过使用,typeof还可以定义可以粘贴到代码中任何位置的代码段.
使用@weakify和@strongify来自libExtObjC有助于简化有时在块周围必须做的"弱强舞".OP引用了这篇文章.
例!
__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;
Run Code Online (Sandbox Code Playgroud)
与...
@weakify(self, delegate, field, viewController);
[viewController respondToSelector:@selector(buttonPressed:) usingBlock:^(id receiver){
@strongify(self, delegate, field, viewController);
Run Code Online (Sandbox Code Playgroud)
你的解释是正确的.但是,我发现当它以这种方式编写时,读起来有点令人困惑.之后我更喜欢它有一个额外的空间typeof(self):
__weak typeof(self) weakSelf = self;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
18159 次 |
| 最近记录: |