将保留对象分配给弱属性 [英] Assigning retained object to weak property

查看:81
本文介绍了将保留对象分配给弱属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Xcode 6 ,并且已经在其中创建了具有UITableViewcustom Cell的应用. 这是我的custom cell

I am using Xcode 6 and I have created my app with a UITableView and a custom Cell in it. This is my custom cell

@interface SuggestingTableViewCell : UITableViewCell

@property (nonatomic, weak) IBOutlet SuggestedSeriesView *seriesOne;
@property (nonatomic, weak) IBOutlet SuggestedSeriesView *seriesTwo;
@property (nonatomic, weak) IBOutlet SuggestedSeriesView *seriesThree;
@property (nonatomic, weak) IBOutlet SuggestedSeriesView *seriesFour;

@end

如您所见,我对UIView的子类SuggestedSeriesView有四个IBOutets. 在TableView DataSource方法中,我创建了这些SuggestedSeriesView并将其分配为:

As you can see I have four IBOutets to a SuggestedSeriesView that is a subclass of UIView. In the TableView DataSource methods I have created these SuggestedSeriesView and assign them like:

cellIdentifier = suggestionCell;
SuggestingTableViewCell *suggesting = (SuggestingTableViewCell *)[tableView dequeueReusableCellWithIdentifier:suggestionCell];
Series *ser1 = series[0];
suggesting.seriesOne = [[SuggestedSeriesView alloc] initWithFrame:suggesting.seriesOne.bounds
                                                    andSeriesData:@{JV_SERIES_IMAGE_URL : ser1.imageURL,
                                                                    JV_SERIES_TITLE : ser1.title}];
Series *ser2 = series[1];
suggesting.seriesTwo = [[SuggestedSeriesView alloc] initWithFrame:suggesting.seriesTwo.bounds
                                                    andSeriesData:@{JV_SERIES_IMAGE_URL : ser2.imageURL,
                                                                    JV_SERIES_TITLE : ser2.title}];
Series *ser3 = series[2];
suggesting.seriesThree = [[SuggestedSeriesView alloc] initWithFrame:suggesting.seriesThree.bounds
                                                      andSeriesData:@{JV_SERIES_IMAGE_URL : ser3.imageURL,
                                                                      JV_SERIES_TITLE : ser3.title}];
Series *ser4 = series[3];

suggesting.seriesFour = [[SuggestedSeriesView alloc] initWithFrame:suggesting.seriesFour.bounds
                                                     andSeriesData:@{JV_SERIES_IMAGE_URL : ser4.imageURL,
                                                                     JV_SERIES_TITLE : ser4.title}];

编译器给我警告:

为保留的对象分配弱属性;对象将在分配后释放

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

为什么会发生这种情况,SuggestedSeriesView被保留在cell中,因为它没有IBOutlet?

Why this is happening to the SuggestedSeriesView gets retained by the cell because it has no IBOutlet?

感谢您的帮助.

推荐答案

发生这种情况是因为您的属性很弱,这意味着它们将不会保留任何内容,只能引用内容.

This happens because your properties are weak, this means they will not retain anything, they can only reference stuff.

IBOutlet等于void,这只是xcode提示它可以在界面构建器上连接"的提示.

IBOutlet is equal to void, it is just a hint for xcode to tell it "this can be connected on the interface builder".

之所以界面构建器的属性类型为弱而IBOutlet的原因是,它们由情节提要的View控制器的视图本身保留,因此,如果您在界面构建器中创建视图控制器,然后添加视图,然后在代码中将此视图链接起来,您的属性不必很强,因为其中一个视图已经保留了该属性.

The reason why properties from the interface builder are of type weak and IBOutlet is because, they are retained by the storyboard's View controller's view itself, so if you make a view controller in the interface builder, and add a view, and THEN link this view in code your property doesn't have to be strong, since its already retained by the one of the views.

您应该将这些属性更改为

You should change those properties to

@property (nonatomic, strong) SuggestedSeriesView *seriesOne;
@property (nonatomic, strong) SuggestedSeriesView *seriesTwo;
@property (nonatomic, strong) SuggestedSeriesView *seriesThree;
@property (nonatomic, strong) SuggestedSeriesView *seriesFour;

这篇关于将保留对象分配给弱属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆