检索核心数据元素的数组

时间:2019-06-13 23:52:28

标签: arrays core-data nsfetchrequest swift5

我想检索核心数据并在日志文件中打印整个阵列。现在,我的函数将数组转换为双精度数组,然后找到所有数字的平均值。我要做的就是在日志文件中打印字符串“ ee”的所有元素的数组

    func performAction() {
        let appD = UIApplication.shared.delegate as! AppDelegate
        let context = appD.persistentContainer.viewContext
        let request = NSFetchRequest<NSFetchRequestResult>(entityName: "Data")
        request.returnsObjectsAsFaults = false
        do {
            let result = try context.fetch(request)
            var retrievedData = [Double]()
            for data in result as! [NSManagedObject] {
                if let value = data.value(forKey: "ee") as? String {
                    retrievedData.append(Double(value) ?? 0)
                }
            }

            let arraySum = retrievedData.reduce(0, +)
            print(arraySum/Double(retrievedData.count))
            let venus = (arraySum/Double(retrievedData.count))
            average.text = String("\(venus)")

        } catch {
            print("Failed")
        }
    }

0 个答案:

没有答案