这是在Objective-C的weakify模式中使用的。我的猜测是:用名称“weakSelf”和self的类型(例如MyViewController)分配一个弱引用给self。如果我猜对了,而且这对你来说很显然:我想要绝对确认正确性。谢谢。
我的猜测是:将一个名为
weakSelf
、类型为self
(例如MyViewController
)的弱引用赋给自身。
是的,几乎就是这个意思。类型为self
的正确写法应该是 MyViewController*
(加了一个星号),而不是 MyViewController
。
使用这种语法的想法是为了避免简单地写成:
MyViewController __weak *weakSelf = self;
使重构代码更加容易。使用typeof
还可以定义一个代码片段,可粘贴到代码的任何位置。
MyViewController * __weak weakSelf = self;
。 - CommaToast@weakify
和 @strongify
从 libExtObjC 可以简化有时需要在块周围执行的 "弱-强舞蹈"。
例子!__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;
对抗...
@weakify(self, delegate, field, viewController);
[viewController respondToSelector:@selector(buttonPressed:) usingBlock:^(id receiver){
@strongify(self, delegate, field, viewController);
MyDelegate *delegate = [[MyDelegate alloc] init];
直到当前方法结束,我将拥有对它的强引用。如果viewController
上的属性是弱引用,那也无所谓,如果我没有将其弱化,仍然会创建一个保留循环。 - CrimsonChristypeof(self)
后面加上一个额外的空格:__weak typeof(self) weakSelf = self;
doSomething() { [weak self] (other, parameters) in doOtherStuff() }
。 - Zev Eisenberg根据您的编译器设置,您可能会收到一个警告“表达式后应有分号”。 您可以通过像这样更改为使用__typeof来解决此问题:__typeof(self) __weak weakSelf = self;
感谢Leo Natan和这个问题:https://dev59.com/VlwY5IYBdhLWcg3w2K3c#32145709。
libextobjc/EXTScope
,它不是 LLDB 的一部分。请参见:weakify。 - zaph