iOS如何从数组中解析Swift数据库中的数据

时间:2018-06-19 05:14:24

标签: ios arrays swift parse-platform

我有一个问题,我在一个类中有数据库,而我这个类我有一行有12个值,这些都不会改变。 所以我想加载数组中的所有数据。

现在我为单独的变量加载数据。 我的代码:

var JAN = Double()
var FEB = Double()
var MAR = Double()
var APR = Double()
var MAY = Double()
var JUN = Double()
var JUL = Double()
var AUG = Double()
var SEP = Double()
var OCT = Double()
var NOV = Double()
var DEC = Double()

加载数据的代码:

     let query = PFQuery(className: "result")
    query.findObjectsInBackground { (objects, error) in
        for object in objects! {
            self.JAN = object["JAN"] as! Double
            self.FEB = object["FEB"] as! Double
            self.MAR = object["MAR"] as! Double
            self.APR = object["APR"] as! Double
            self.MAY = object["MAY"] as! Double
            self.JUN = object["JUN"] as! Double
            self.JUL = object["JUL"] as! Double
            self.AUG = object["AUG"] as! Double
            self.SEP = object["SEP"] as! Double
            self.OCT = object["OCT"] as! Double
            self.NOV = object["NOV"] as! Double
            self.DEC = object["DEC"] as! Double
        }

    }

我在单独的变量中加载的所有数据,我想保存在一个数组中。 所以我想把所有数据加载到一个数组中,我该怎么做?

2 个答案:

答案 0 :(得分:0)

可以使用一系列的游戏
假设您有object

中的单objects
var array = [(name: String, value: Double)]()
let query = PFQuery(className: "result")
query.findObjectsInBackground { (objects, error) in
    if let object in objects?.first {
        array.append((name: "JAN", value: object["JAN"] as! Double))
        array.append((name: "FEB", value: object["JAN"] as! Double))
        ...
    }
}
// use it as
for each in array {
    print("\(each.name): \(each.value)")
}

您还可以添加所有(键,值)对,而无需指定来自object的键,具体取决于您object类型。如果是字典那么

query.findObjectsInBackground { (objects, error) in
    if let object in objects?.first {
        for key in object.keys {
            array.append((name: key, value: object[key] as! Double))
        }
    }
}

答案 1 :(得分:0)

let query = PFQuery(className: "result")
        query.findObjectsInBackground { (objects, error) in
             ServerData.init(JAN: object["JAN"], FEB: object["FEB"], MAR: object["MAR"], APR: object["APR"], MAY: object["MAY"], JUN: object["JUN"], JUL: object["JUL"], AUG: object["AUG"], SEP: object["SEP"], OCT: object["OCT"], NOV: object["NOV"], DEC: object["DEC"])

        }

选择serverData struct class

import Foundation
struct ServerData {

var JAN :Double?
var FEB : Double?
var MAR : Double?
var APR : Double?
var MAY : Double?
var JUN : Double?
var JUL : Double?
var AUG : Double?
var SEP : Double?
var OCT : Double?
var NOV : Double?
var DEC : Double?

    init(JAN: Double,FEB: Double,MAR:Double,APR:Double,MAY:Double,JUN:Double,JUL:Double,AUG:Double,SEP:Double,OCT:Double,NOV:Double,DEC: Double) {
        self.JAN = JAN
        self.FEB = FEB
        self.MAR = MAR
        self.APR = APR
        self.MAR = MAY
        self.JUN = JUN
        self.JUL = JUL
        self.AUG = AUG
        self.SEP = SEP
        self.OCT = OCT
        self.NOV = NOV
        self.DEC = DEC
    }
}