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属性里。

各位都知道了吧。

欢迎加群互相学习,共同进步。QQ群:iOS: 58099570 | Android: 572064792 | Nodejs:329118122 做人要厚道,转载请注明出处!













本文转自张昺华-sky博客园博客,原文链接:http://www.cnblogs.com/sunshine-anycall/p/3436659.html ,如需转载请自行联系原作者


相关文章
成功解决AttributeError: ‘PathCollection‘ object has no property ‘n_levels‘
成功解决AttributeError: ‘PathCollection‘ object has no property ‘n_levels‘
|
数据可视化 Python
AttributeError: ‘Rectangle‘ object has no property ‘normed‘ 解决方法 matplotlib绘图
AttributeError: ‘Rectangle‘ object has no property ‘normed‘
848 0
AttributeError: ‘Rectangle‘ object has no property ‘normed‘ 解决方法 matplotlib绘图
|
Python
Python 修改tuple元组提示:TypeError: ‘tuple‘ object does not support item assignment. 问题原因
Python 修改tuple元组提示:TypeError: ‘tuple‘ object does not support item assignment. 问题原因
827 0
|
XML 数据格式
SAP Fiori Elements - object detail batch roundtrip triggered by binding property in embedded xml vie
SAP Fiori Elements - object detail batch roundtrip triggered by binding property in embedded xml vie
86 0
SAP Fiori Elements - object detail batch roundtrip triggered by binding property in embedded xml vie
|
Python
如何查看某个ABAP user针对某个authorization object的assignment status
如何查看某个ABAP user针对某个authorization object的assignment status
133 0
成功解决AttributeError: ‘PathCollection‘ object has no property ‘n_levels‘ daiding
成功解决AttributeError: ‘PathCollection‘ object has no property ‘n_levels‘ daiding
成功解决AttributeError: ‘PathCollection‘ object has no property ‘n_levels‘
成功解决AttributeError: ‘PathCollection‘ object has no property ‘n_levels‘
成功解决random.py"之TypeError: 'range' object does not support item assignment
成功解决random.py"之TypeError: 'range' object does not support item assignment
成功解决random.py"之TypeError: 'range' object does not support item assignment 解决问题
成功解决random.py"之TypeError: 'range' object does not support item assignment
成功解决random.py"之TypeError: 'range' object does not support item assignment 解决问题
成功解决random.py"之TypeError: 'range' object does not support item assignment 解决问题