查询从Parse.com到数组的数组

时间:2015-11-11 21:43:00

标签: ios swift parse-platform

我正在尝试从Parse.com查询数组到空数组。

我的空数组:

var commentsArray = [NSArray]()

我的查询代码:

var query1 = PFQuery(className: "currentUploads")
        query1.whereKey("objectId", equalTo: "ImxZwVx7L8")
        query1.findObjectsInBackgroundWithBlock{
            (objects: [AnyObject]?, error: NSError?) -> Void in
            if error == nil {
                if let objects = objects as? [PFObject] {
                    for object in objects {

                        //For each object in the class object, append it to myArray
                        if let dataArray = object["comments"] as? NSArray {
                            self.commentsArray.append(dataArray)

                            println("codeBelow")
                            println(self.commentsArray)
                            println(self.commentsArray.count)
                        }
                    }
                }
            } else {
                println("\(error?.userInfo)")
            }
        }

在输出中我得到了这个:

codeBelow
[(
    abc,
    def,
    def,
    def,
    def,
    def,
    def,
    def,
    def,
    def,
    def,
    def,
    def,
    def,
    def,
    def,
    def,
    def,
    def,
    def,
    def,
    def,
    def,
    def,
    def,
    def,
    def,
    def,
    def,
    def,
    def,
    def,
    def,
    def,
    def,
    def,
    tdrthdrthdrthdrthdrhtrthdrthdrthdrthdrthdrthdrthdrthdrth,
    "jdrthdrthdrthdrthdrthdrthdrthdrthdrthdrthdrthdrthdrthdrthdrthdrthdrthdrthdrt dhrt hdrthdrthdrthdrh 5hsr5d r5hdr5h drthr t hdrt hkl"
)]
1

正如你所看到的,输出计数给了我1,但它应该给出38.如果我做println(dataArray.count)它给我输出38.为什么?有什么问题?

1 个答案:

答案 0 :(得分:1)

首先,创建一个空数组:

var commentsArray: [String] = [String]()

现在查询:

var query = PFQuery(className:"currentUploads")
        query.whereKey("objectId", equalTo:"ImxZwVx7L8")
        query.findObjectsInBackgroundWithBlock { (objects: [AnyObject]?, error: NSError?) -> Void in
            if error == nil {
                if let objects = objects as? [PFObject] {
                    for object in objects {
                        var list: AnyObject? = object.objectForKey("comments")
                        self.commentsArray = list! as! NSArray as! [String]
                        self.tableView.reloadData()
                    }

                }
            } else {
                println("\(error?.userInfo)")
            }
        }