获得双打的迅速数组的总和 [英] Getting the sum of an array of doubles in swift

查看:125
本文介绍了获得双打的迅速数组的总和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我从核心数据读取和转换为双打串数字数组。我希望得到他们的总和,一旦我这样做,但我得到一个错误。我已经试过这样的:

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?

推荐答案

看你的code,我推断出结果是字典的数组,每个字典有一个 totalWorkTimeInHoursString 键,它的值是一个字符串,再一个双号的presentation。

Looking at your code, I deduce that results is an array of dictionaries, and each dictionary has a totalWorkTimeInHoursString key, whose value is a string representation of a double number.

我就用了一下功能的方法,用解决过滤地图减少

I would solve it using a bit of functional approach, using filter, map and reduce:

if results.count > 0 {
    var res = results as [NSDictionary]

    var result = res.filter { $0["totalWorkTimeInHoursString"] is String }
        .map { ($0["totalWorkTimeInHoursString"] as NSString).doubleValue }
        .reduce (0, +)
}

什么是code的作用:

What the code does:


  • 转换结果数组的数组的NSDictionary

  • 通过移除所有元素(这是字典)不具有 totalWorkTimeInHoursString 键,或者其相应的值不是一个字符串过滤阵列

  • 数组中的每个元素转换为双精度,通过抽取相应于 totalWorkTimeInHoursString 键的值,转换为的NSString 再到双击

  • 适用减少方法,将 + 运算符来总结造成的阵列

  • convert the results array into an array of NSDictionary
  • filter the array by removing all elements (which are dictionaries) not having the totalWorkTimeInHoursString key, or whose corresponding value is not a string
  • convert each element of the array to a double, by extracting the value corresponding to the totalWorkTimeInHoursString key, converting to NSString and then to Double
  • apply the reduce method, passing the + operator to sum up the resulting array of Double

这是我测试我的解决方案与数据,在操场:

This is the data I tested my solution with, in a playground:

var results: NSArray = [
    ["totalWorkTimeInHoursString": "10.0"],
    ["totalWorkTimeInHoursString": "5.0"]
]

这篇关于获得双打的迅速数组的总和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆