条形图使用UserDefaults持久保存图表值

时间:2018-05-01 02:44:25

标签: swift ios-charts

我正在尝试使用UserDefaults保留条形图值,以便每次调用应用程序时它都不会显示空白图表。

let defaults = UserDefaults.standard        
    let dataSet = BarChartDataSet(values: [entry1, entry2, entry3, entry4], label: "Widgets Type")   
    let data = BarChartData(dataSets: [dataSet])  
    let encodedData = NSKeyedArchiver.archivedData(withRootObject: data)

    defaults.set(encodedData, forKey: "encoded data")
    defaults.synchronize()

    if let decodedData = defaults.object(forKey: "encoded data") as? Data {
        let data = NSKeyedUnarchiver.unarchiveObject(with: decodedData )
        barChart.data = data as? ChartData

但是当我运行我的应用程序时,我收到以下错误消息

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Charts.BarChartData encodeWithCoder:]: unrecognized selector sent to instance

0 个答案:

没有答案