获取要用于图表的核心数据
Posted
技术标签:
【中文标题】获取要用于图表的核心数据【英文标题】:Fetching Core Data to use for a chart 【发布时间】:2016-08-22 02:21:25 【问题描述】:是否有任何将 Core Data 用于图表的示例?我一直在尝试示例项目,但没有成功。我了解如何在声明数组时使用数据,但在使用获取的结果时会很困难。
这是我找到的图表视图示例:
var chartLegend = ["11-14", "11-15", "11-16", "11-17", "11-18", "11-19", "11-20"]
var chartData = [70, 80, 76, 88, 90, 69, 74]
func numberOfBarsInBarChartView(barChartView: JBBarChartView!) -> UInt
return UInt(chartData.count)
func barChartView(barChartView: JBBarChartView!, heightForBarViewAtIndex index: UInt) -> CGFloat
return CGFloat(chartData[Int(index)])
func barChartView(barChartView: JBBarChartView!, colorForBarViewAtIndex index: UInt) -> UIColor!
return (index % 2 == 0) ? UIColor.lightGrayColor() : UIColor.whiteColor()
func barChartView(barChartView: JBBarChartView!, didSelectBarAtIndex index: UInt)
let data = chartData[Int(index)]
let key = chartLegend[Int(index)]
infoLabel.text = "Weather on \(key): \(data)"
func didDeselectBarChartView(barChartView: JBBarChartView!)
infoLabel.text = ""
这是我用于获取数据的内容:
let managedObjectContext = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext
var fetchedResultController: NSFetchedResultsController = NSFetchedResultsController()
func getFetchedResultController() -> NSFetchedResultsController
fetchedResultController = NSFetchedResultsController(fetchRequest: taskFetchRequest(), managedObjectContext: managedObjectContext, sectionNameKeyPath: nil, cacheName: nil)
return fetchedResultController
func taskFetchRequest() -> NSFetchRequest
let fetchRequest = NSFetchRequest(entityName: "Entity")
let timeSortDescriptor = NSSortDescriptor(key: "attribute1",
ascending: true, selector: #selector(NSString.caseInsensitiveCompare(_:)))
let milesSortDescriptor = NSSortDescriptor(key: "attribute2",
ascending: true, selector: #selector(NSString.caseInsensitiveCompare(_:)))
fetchRequest.sortDescriptors = [timeSortDescriptor, milesSortDescriptor]
return fetchRequest
这就是我能够检索核心数据并将其用于在表格中查看的方式。它如何用于在图表中查看?
let managedObjectContext = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext
var fetchedResultController: NSFetchedResultsController = NSFetchedResultsController()
override func viewDidLoad()
super.viewDidLoad()
fetchedResultController = getFetchedResultController()
fetchedResultController.delegate = self
do
try fetchedResultController.performFetch()
catch _
// MARK:- Retrieve Tasks
func getFetchedResultController() -> NSFetchedResultsController
fetchedResultController = NSFetchedResultsController(fetchRequest: taskFetchRequest(), managedObjectContext: managedObjectContext, sectionNameKeyPath: nil, cacheName: nil)
return fetchedResultController
func taskFetchRequest() -> NSFetchRequest
let fetchRequest = NSFetchRequest(entityName: "RunTime")
let timeSortDescriptor = NSSortDescriptor(key: "time",
ascending: true, selector: #selector(NSString.caseInsensitiveCompare(_:)))
let milesSortDescriptor = NSSortDescriptor(key: "miles",
ascending: true, selector: #selector(NSString.caseInsensitiveCompare(_:)))
fetchRequest.sortDescriptors = [timeSortDescriptor, milesSortDescriptor]
return fetchRequest
override func didReceiveMemoryWarning()
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
// MARK: - TableView data source
override func numberOfSectionsInTableView(tableView: UITableView) -> Int
let numberOfSections = fetchedResultController.sections?.count
return numberOfSections!
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
let numberOfRowsInSection = fetchedResultController.sections?[section].numberOfObjects
return numberOfRowsInSection!
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)
let runtime = fetchedResultController.objectAtIndexPath(indexPath) as! RunTime
cell.textLabel?.text = runtime.time
cell.detailTextLabel!.text = runtime.miles
return cell
// MARK: - TableView Deleteƒ
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath)
let managedObject:NSManagedObject = fetchedResultController.objectAtIndexPath(indexPath) as! NSManagedObject
managedObjectContext.deleteObject(managedObject)
do
try managedObjectContext.save()
catch _
// MARK: - TableView Refresh
func controllerDidChangeContent(controller: NSFetchedResultsController)
tableView.reloadData()
【问题讨论】:
您是否创建了自己的实体并生成了相应的 swift 文件?您将数据保存在哪里? 是的,我创建了自己的实体。在上面,我编辑了我的帖子并添加了我在 tableview 中检索核心数据所做的工作 【参考方案1】:在 ios 13 上使用 SwiftUI,现在使用 @FetchRequest 属性包装器应该非常容易。
【讨论】:
以上是关于获取要用于图表的核心数据的主要内容,如果未能解决你的问题,请参考以下文章