Assigning retained object to weak property object

 

Assigning retained object to weak property object will be released after assignment 

 

 

 

Assigning retained object to weak property object will be released after assignment

在ARC中,如果添加了weak的属性。初始化了相关的object然后给这个属性赋值的时候就会看到Xcode给出这个提示。

这个时候可以这么处理:在别的地方已经retain这个object的引用。

复制代码

@property (nonatomic, weak) KGModalContainerView *containerView;
...
-(void)viewDidLoad {
    [super viewDidLoad];
    KGModalContainerView *myContainerView = [[KGModalContainerView alloc] initWithFrame:containerViewRect]; // This is a strong reference to that view
    [self.view addSubview:myContainerView]; //Here self.view retains myContainerView
    self.containerView = myContainerView; // Now self.containerView has weak reference to that view, but if your self.view removes this view, self.containerView will automatically go to nil.

 // In the end ARC will release myContainerView, but it's retained by self.view and weak referenced by self.containerView
}

复制代码

在这个例子中,先初始化了ContainerView。然后把这个View放到self.view的子View中,也就是retain了ContainerView。

然后再把ContainerView赋值给weak属性里。

各位都知道了吧。

转载于:https://my.oschina.net/fadoudou/blog/719561

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值