I created a core data data model and now I want to total/sum one of the attributes. The data model is as follows:
import CoreData
@objc(Aktie)
class Aktie: NSManagedObject
{
@NSManaged var deletedDate: Date?
@NSManaged var id: NSNumber!
@NSManaged var title: String?
@NSManaged var point: String?
}
The code for the ViewController to show the total is:
import UIKit
import CoreData
var points = [Aktie]()
var firstLoad = true
class TotalPointsViewController: UIViewController {
@IBOutlet weak var totalPoints: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
if(firstLoad)
{
firstLoad = false
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let context: NSManagedObjectContext = appDelegate.persistentContainer.viewContext
let request = NSFetchRequest<NSFetchRequestResult>(entityName: "Aktie")
do {
let results: NSArray = try context.fetch(request) as NSArray
for result in results {
let aktie = result as! Aktie
points.append(aktie)
}
}
catch
{
print("Fetch Failed")
}
}
}
// Mark: Add total of points
func calculateSumPoints() -> [Aktie] {
let sum = 0
for punt in points.point {
sum += Int(punt)
}
totalPoints.text = "\(sum)"
}
}
What am I doing wrong?