问题
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.
回答1:
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;
回答2:
You are creating an object at the same time as assigning it to a weak property. At this point, nothing has a strong reference to it, so under the rules of ARC it should be immediately nilled out. (Note than when running debug builds, this does not happen straight away).
When loading from the storyboard the object is created, added as a subview and then assigned to the outlet. The superview has a strong reference so this is fine.
To mirror this behaviour without changing your outlet property type (though to be honest, there's not much harm in that nowadays) you should assign your new object to a local variable, then add it to a view, then assign it to the outlet property.
回答3:
@interface SuggestingTableViewCell : UITableViewCell
@property (nonatomic, weak) IBOutlet SuggestedSeriesView *seriesOne;
@end
cellIdentifier = suggestionCell;
SuggestingTableViewCell *suggesting = (SuggestingTableViewCell *)[tableView dequeueReusableCellWithIdentifier:suggestionCell];
Series *ser1 = series[0];
SuggestedSeriesView * strongSeriesOne = [[SuggestedSeriesView alloc] initWithFrame:suggesting.seriesOne.bounds andSeriesData:@{JV_SERIES_IMAGE_URL : ser1.imageURL, JV_SERIES_TITLE : ser1.title}];
suggesting.seriesOne = strongSeriesOne;
来源:https://stackoverflow.com/questions/26582530/assigning-retained-object-to-weak-property