我想为我的 objective-c 项目使用 iOS 图表。由于 UI 完全用代码编写,我不想专门为图 TableView 创建一个 nib 文件。但是,创建 LineChartView 的简单 init 或 initWithFrame 给我 nil
//Declare chartView property in header
@property (nonatomic, weak) LineChartView* chartView;
//Call to init chart
CGRect frame = CGRectMake(0, 0, CGRectGetWidth(self.view.bounds),
CGRectGetHeight(self.view.bounds));
self.chartView = [[LineChartView alloc] initWithFrame: frame];
调用上面的代码后,self.chartView 为nil。
最佳答案
根据我的经验,当您使用 Init 方法分配对象时,您需要删除 Weak
属性,只有 nonatomic
会起作用。
@property (nonatomic, weak) LineChartView *lineChart;
这个应该换成
@property (nonatomic) LineChartView *lineChart;
就好像你创建了弱属性,它会在分配后释放。
当你犯这种类型的错误时,XCode
会抛出如下警告:
warning: assigning retained object to weak property; object will be released after assignment [-Warc-unsafe-retained-assign] self.lineChart = [[LineChartView alloc] initWithFrame:CGRectMake(0, 0, 320, 320)]; ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 1 warning generated.
因此,当您在其中分配任何 retain
对象时,in-sort 永远不要使用 weak
。
希望这会有所帮助!
关于iOS 图表 - 如何以编程方式初始化 LineChartView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46082833/