ios - 将保留的对象分配给弱属性

标签 ios objective-c retain

我正在使用 Xcode 6,我创建了一个包含 UITableViewcustom 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

如您所见,我有四个 IBOutetsSuggestedSeriesView,它是 UIView 的子类。 在 TableView DataSource 方法中,我创建了这些 SuggestedSeriesView 并将它们分配为:

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 会因为没有 IBOutlet 而被 cell 保留?

感谢您的帮助。

最佳答案

发生这种情况是因为你的属性很弱,这意味着它们不会保留任何东西,它们只能引用东西。

IBOutlet 等于 void,它只是提示 xcode 告诉它“这可以在界面构建器上连接”。

Interface Builder 中的属性类型为 weak 和 IBOutlet 的原因是,它们由 Storyboard 的 View Controller View 本身保留,因此如果您在 Interface Builder 中创建一个 View Controller,并添加一个 View ,然后在代码中链接此 View 您的属性不必很强,因为它已经被其中一个 View 保留。

您应该将这些属性更改为

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

关于ios - 将保留的对象分配给弱属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26582530/

相关文章:

ios - 我可以删除 Library 下的整个开发者文件夹吗

objective-c - 从另一个类访问 NSWindow

objective-c - 是否可以在 cocoa xib/nib 中嵌入终端窗口?

ios - 代理的属性 "assign"和 "retain"

javascript - 未附加到 View 层次结构时的 WKWebKit javascript 执行

iphone - 如何保存来自不同 View Controller 的数据并将其加载到 iOS 中的另一个 View Controller 中

objective-c - 如何在NSString中使用文本和变量的组合?

objective-c - iOS6 中的 TableView 圆角半径不同

objective-c - CGImageRef 属性保留或不保留

iphone - iOS ARC - 弱属性和强属性