Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assigning retained object to weak property

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.

like image 791
YYfim Avatar asked Oct 27 '14 07:10

YYfim


People also ask

What happens when an object is referenced weakly in a class?

The object you are referencing weakly only lives on because some object holds a strong reference to it once that object loses its reference this object will be destroyed. →Delegate properties, referenced weakly to avoid retain cycles. →IBOutlets, Subviews because these views are strongly held by the superview.

What happens if you destroy an object with a strong reference?

Any data that you assign to this property will not be destroyed as long as you or any other object points to it with a strong reference. In Objective-C, an object is kept alive as long as it has at least one strong reference to it from another object.

How do you keep an object alive in Objective-C?

In Objective-C, an object is kept alive as long as it has at least one strong reference to it from another object. In non-ARC code strong is just a synonym for retain. A variable maintains a strong reference to an object only as long as that variable is in scope, or until it is reassigned to another object or nil.

What is object ownership in Objective-C?

Many languages accomplish memory management through garbage collector, but Objective-C uses a more efficient alternative called object ownership or reference counting. strong / retain : Declaring strong means that you want to “own” the object you are referencing.


1 Answers

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;
like image 56
Pochi Avatar answered Sep 28 '22 09:09

Pochi