gpt4 book ai didi

ios - 在 iOS 中,头文件的 @interface 大括号内的 iVar 声明是强还是弱?

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:43:08 25 4
gpt4 key购买 nike

下面的声明和调用是强引用还是弱引用?我知道 NSNotificationCenter block 内的强引用会导致保留循环,所以我正在努力避免这种情况。

声明:

@interface MPOCertifiedAccountsViewController : MPORootViewController <UITableViewDataSource, UITableViewDelegate> {

UITableView *certifiedTableView;
}

调用:

- (id)init
{
self = [super init];

if (self) {

[[NSNotificationCenter defaultCenter] addObserverForName:MPOFriendManagerManagerDidFinishRefreshingLocal
object:nil
queue:[NSOperationQueue mainQueue]
usingBlock:^(NSNotification *note) {

[certifiedTableView reloadData];

}];
}

return self;
}

最佳答案

默认情况下所有实例变量都是strong。然而,这与这里无关,因为

[certifiedTableView reloadData];

事实上

[self->certifiedTableView reloadData];

并且保留self,而不是实例变量。所以你在这里有一个保留周期,与 certifiedTableView 是强实例变量还是弱实例变量无关。

您可以使用创建对 self 的弱引用的众所周知的技术来解决这个问题:

__weak typeof(self) weakSelf = self;

在 block 中使用:

typeof(self) strongSelf = weakSelf;
if (strongSelf != nil) {
[strongSelf->certifiedTableView reloadData];
}

您还应该考虑使用属性 而不是实例变量。使用 self.certifiedTableView,您会立即看到 self 已保留。

关于ios - 在 iOS 中,头文件的 @interface 大括号内的 iVar 声明是强还是弱?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23686534/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com