我试图加载Parse数据并在数组中设置为结果,我检查了几个问题,但很多代码已经过时,所以我卡住了,我怎么能在数组中设置查询?继承人代码:
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 )
}
}
}
答案 0 :(得分:1)
这里的代码有点不对,我就是这样做的。
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)
}
}
我发现的另一个问题是我认为你试图保存一个对象数组,而不是一个类的属性数组。上面的方法只是将名为column
的对象属性存储在一个数组中。如果你想将PFObject存储在一个数组中,那么你将不得不改变你创建的array
变量来键入PFObject,如下所示:var array: [PFObject] = [PFObject]()
答案 1 :(得分:0)
我认为你需要这段代码:
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 )
}
}
}