0

Im trying to load Parse data and set in a array as result, i check a few questions but many codes are outdate so I'm stuck, how i can set the query in a array? heres the code:

var array [String] = []

func loadData() {

    var query = PFQuery(className: "ParseClass")

    query.orderByAscending("column")

    query.findObjectsInBackgroundWithBlock { (objects: [AnyObject]?, error: NSError?) -> Void in

        if error == nil {

            self.array.append( objects! )// error here

        } else {

            println( error?.userInfo )
        }
    }

}
3
  • Have you tried printing the objects yet? Commented Aug 4, 2015 at 17:06
  • i can't its give a error "cannot invoke 'append' with a argument list of type ([AnyObject])" Commented Aug 4, 2015 at 17:09
  • Did you follow what the Parse blog did? It seems like you set where PFObject should be in your block function to AnyObject. link Commented Aug 4, 2015 at 17:11

2 Answers 2

1

The code is a little wrong here, this is how I would come around to doing it.

var array: [String] = [String]()
var query = PFQuery(className: "ParseClass")

func loadData() {

    query.orderByAscending("column")

    query.findObjectsInBackgroundWithBlock{ (objects: [AnyObject]?, error: NSError?) -> Void in
            if error == nil {
                if let objects = objects as? [PFObject] {
                    for object in objects {
                           self.array.append(object.objectForKey("column") as! String)
                    }
}

Another problem that I spotted is that I think you trying to save an array of objects and not an array of an attribute of the class. My method above just stores the objects attribute called column in an array. If you want to store the PFObjects in an array then you are going to have to change the array variable you created to type PFObject like this : var array: [PFObject] = [PFObject]()

Sign up to request clarification or add additional context in comments.

Comments

0

I think you need this code:

var array = NSMutableArray()   //Change your array type to NSMutableArray

func loadData1() {

    var query = PFQuery(className: "ParseClass")

    query.orderByAscending("column")

    query.findObjectsInBackgroundWithBlock { (objects: [AnyObject]!, error: NSError?) -> Void in

        if error == nil {

            //loop your objects array
            for object in objects{

                let yourObject = object as! PFObject
                //add your element into array
                self.array.addObject(yourObject)
            }

            let tempArr :NSArray = self.array.reverseObjectEnumerator().allObjects
            self.array = NSMutableArray(array: tempArr)

        } else {

            println( error?.userInfo )
        }
    }

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.