Swift:从Parse中检索2D数组

时间:2015-02-26 11:38:00

标签: swift parse-platform

我是编程新手。刚开始一个月回来。我正在开发一个快速的项目,并希望从Parse中检索数据。

我有两个选项,要么是表格中的数据,要么是1D数组,或者我可以以2D数组的形式使用它。我在检索两个数组时遇到问题。因为我没有收视率,所以我无法附上照片。所以将解释我的数据库如下: 类:FruitQuantityMatrix 列:ObjectID,Sr.Number,fruitName,fruitQuantityEachDay。 行:Apple,Orange,Cherry,Banana,Grape,Guava,Kiwi

否则,如果可能的话,我也可以使用2D数据进行检索。在这种情况下,数据将类似于下面的矩阵。但我认为使用一维阵列会更好。但出于学习目的,我想了解它在Parse中是如何工作的。

var QMatrix:[[String]] = [
        ["11","12","13","14","15","16","17"],
        ["10","22","23","24","25","26","27"],
        ["31","32","33","34","35","36","37"],
        ["41","42","43","44","45","46","47"],
        ["51","52","53","54","55","56","57"],
        ["61","62","63","64","65","66","67"],
        ["71","72","73","74","75","76","77"]
    ]

我尝试使用我用来检索图像的代码,但它不起作用。代码如下:

var imageResources : Array<UIImage> = []     
func loadQuantity() {

        var query = PFQuery(className: "FruitQuantityMatrix")
        query.orderByDescending("serialNo")

        query.findObjectsInBackgroundWithBlock ({(objects:[AnyObject]!, error: NSError!) in
            if(error == nil){

                self.getQuantityData(objects as [PFObject])
            }
            else{
                println("Error in retrieving \(error)")
            }

        })//findObjectsInBackgroundWithblock - end


    }

    func getQuantityData(objects: [PFObject]) {
        for object in objects {

            let qty = object["fruitQuantityEachDay"] as PFObject
            println(qty)

            qty.getDataInBackgroundWithBlock({
                (quantityData: NSData!, error: NSError!) -> Void in
                if (error == nil) {
                    let fruitQuantityEachDay = self.quantityResources.append(fruitQuantityEachDay!)
                    println(fruitQuantityEachDay!)
                }

            })//getDataInBackgroundWithBlock - end

        }//for - end
    }

如果您能为我提供代码,我将不胜感激。但是我更喜欢简短的解释,因为我想自己编程。我发布的每个其他地方(Facebook社区)都需要钱来教我。我没有钱,想学习。所以任何帮助将不胜感激。 谢谢。

1 个答案:

答案 0 :(得分:1)

Docs所示,您使用此方法从数据库中检索数据:

var query = PFQuery(className:"FruitQuantityMatrix")

query.getObjectInBackgroundWithId("some_id") { object, error in
    if error == nil {
        println(object)
    } 
    else {
        println(error)
    }
}

要访问object中的值,您会将其视为Dictionary

let name = object["fruitName"] as String
let arr = object["fruitQuantityEachDay"] as [String]

如果您想要所有或特定行而不使用id,请改用:

query.whereKey("fruitName", equalTo: "Apple") // Dont use this if you want to retreive all rows

query.findObjectsInBackgroundWithBlock { object, error in 
    let name = object[rowIndex]["fruitName"] as String
}