我使用這個代碼來解析數據檢索圖像...如何查詢的PFFile通過創建時間
if let userPicture = object.valueForKey("Image") as? PFFile {
userPicture.getDataInBackgroundWithBlock({ (imageData: NSData?, error: NSError?) -> Void in
if (error == nil) {
let image = UIImage(data:imageData!)
self.ImageArray.insert(image!, atIndex: 0)
}
else {
self.alert("Error: \(error!) \(error!.userInfo!)", Message: "Make sure you have a secure internet connection")
}
dispatch_async(dispatch_get_main_queue()) {
println("Finished Pictures")
}
})
}
,我使用這個代碼來解析檢索字符串:
var stuffarray = [String]()
var query = PFQuery(className:"ClassName")
query.findObjectsInBackgroundWithBlock {
(objects: [AnyObject]?, error: NSError?) -> Void in
if error == nil {
// The find succeeded.
println("Successfully retrieved \(objects!.count) scores.")
// Do something with the found objects
if let objects = objects as? [PFObject] {
for object in objects {
stuffarray.append(object.valueForKey("Column")! as! String)
}
} else {
// Log details of the failure
println("Error: \(error!) \(error!.userInfo!)")
}
dispatch_async(dispatch_get_main_queue()) {
self.alert("\(stuffarray)", Message: "")
}
}
}
我知道如何通過添加以下代碼來查詢第二個例子:
query.orderByAscending("createdAt")
我的問題是,我該如何查詢第一次考試我在第二個例子中做了同樣的方式嗎?我試着用下面的代碼,但我得到一個錯誤:
userPicture.orderByAscending("createdAt")
我用下面的代碼嘗試,是否被正確返回的文本,但圖像以隨機順序還是回到...
var query = PFQuery(className:"Featured")
query.orderByDescending("createdAt")
query.findObjectsInBackgroundWithBlock {
(objects: [AnyObject]?, error: NSError?) -> Void in
if error == nil {
// The find succeeded.
println("Successfully retrieved \(objects!.count) Items.")
// Do something with the found objects
if let objects = objects as? [PFObject] {
for object in objects {
self.NameArray.insert(object.valueForKey("Text")! as! String, atIndex: 0)
self.ItemNameArray.insert(object.valueForKey("ItemName")! as! String, atIndex: 0)
let userImageFile = object["Image"] as! PFFile
userImageFile.getDataInBackgroundWithBlock {
(imageData: NSData?, error: NSError?) -> Void in
if error == nil {
if let imageData = imageData {
let image = UIImage(data:imageData)
self.imageArray.insert(image!, atIndex: 0)
}
}
dispatch_async(dispatch_get_main_queue()) {
self.loading.hidden = true
self.collectionView.reloadData()
}
}
}
}
} else {
// Log details of the failure
println("Error: \(error!) \(error!.userInfo!)")
}
}
我不知道任何目標-c。請幫我把它轉換成swift。 –
你能澄清你在哪裏看到你的圖像以錯誤的順序返回?它是在數組中還是在您的視圖本身?我可以看到,您正在PFQuery上執行orderByDescending,但是您將結果插入索引爲0的數組中。通過將最後一個對象移動到數組的第一個,可以顛倒查詢的順序。那可能是你的罪魁禍首? – mrbcg
將.append用於.insert atIndex位置 – mrbcg