I have an array of string numbers that I am fetching from core data and converting to doubles. I would like to get their sum once I've done this, but I get an error. I've tried it like this:
override func viewDidLoad() {
super.viewDidLoad()
//CoreData
let appDelegate = UIApplication.sharedApplication().delegate as AppDelegate
let managedContext : NSManagedObjectContext = appDelegate.managedObjectContext!
var fetchRequest = NSFetchRequest(entityName: "Log")
fetchRequest.returnsObjectsAsFaults = false;
var results: NSArray = managedContext.executeFetchRequest(fetchRequest, error: nil)!
if (results.count > 0) {
for res in results {
var totalWorkTimeInHoursString = res.valueForKey("totalWorkTimeInHoursString") as String
//get double value of strings array
var totalWorkTimeInHoursNSString = NSString(string: totalWorkTimeInHoursString)
var totalWorkTimeInHoursNSStringDoubleValue = totalWorkTimeInHoursNSString.doubleValue
lastLogHoursWorked.text = "\(totalWorkTimeInHoursString) hours"
totalHoursWorkedSum.text = "\(totalWorkTimeInHoursNSStringDoubleValue)"
let sum = totalWorkTimeInHoursNSStringDoubleValue.reduce(0,+)
//throws an error saying 'Double' does not have a member named 'reduce'
println(totalWorkTimeInHoursNSStringDoubleValue)
//lists the array of doubles in console successfully
println(sum)
//checking to see if 'sum' works properly
}
}else {
println("zero results returned, potential error")
}
}
Am I approaching this properly? what am I missing?