Assigning retained object to weak property

YYfim picture YYfim · Oct 27, 2014 · Viewed 15.7k times · Source

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

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}];

The compiler gives me the warning that :

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

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

Thanks for the help.

Answer

Pochi picture Pochi · Oct 27, 2014

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

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

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;