2015-02-06 79 views
0

我目前從我的第一個解析步驟開始,但目前我堅持一個非常基礎的點。 有沒有辦法從我的「parseObject」中取回所有「objectID」列表的數組?Parse.com用Swift獲取對象ID

我只是希望得到所有的自動設置從一「表」

回答

3

這裏是一個迅速解決:

 // objectIds is your array to store the objectId values returned from parse 
    // objectId is a String 

    var objectIds:[""] // empty string array 
    func loadDataFromParse() { 
     var query = PFQuery(className:"Record") 
     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 
       for object in objects { 
        objectIds.append(object.objectId as String) 
       } 
      } else { 
       println("\(error)") 
      } 

     } 

    } 


// this function will retrieve a photo in the record with specified objectId 
// and store it in noteImage 

    var noteImage = UIImage() // where retrieved image is stored 
    func loadImageFromParse (objectId: String) { 

     var query = PFQuery(className:"Record") 
     query.whereKey("objectId", equalTo:objectId) 
     query.findObjectsInBackgroundWithBlock { 
      (objects: [AnyObject]!, error: NSError!) -> Void in 
      if error == nil { 
       println("Successfully retrieved \(objects.count) records.") 
       for object in objects { 
        let userImageFile = object["image"] as PFFile! 
        userImageFile.getDataInBackgroundWithBlock { 
         (imageData: NSData!, error: NSError!) -> Void in 
         if error == nil { 
          noteImage = UIImage(data:imageData)! 
          println("Image successfully retrieved") 
         } 
        } 
       } 

      } else { 
       NSLog("Error: %@ %@", error, error.userInfo!) 
      } 
     } 
    } 
0

OBJECTID一個陣列如果我正確理解你的問題,你需要讓你的對象的查詢,例如:

PFQuery *userPhotosQuery = [PFQuery queryWithClassName:@"photos"]; 
[userPhotosQuery whereKey:@"user"equalTo:[PFUser currentUser]]; 
[userPhotosQuery orderByDescending:@"createdAt"]; 

這將返回當前用戶保存的所有照片對象。 您也可以添加其他任何過濾器。 請糾正我如果我錯了或不正確地捕捉到問題。

+3

你應該把它翻譯成swift,因爲這是問題標題所指出的,而且OP已經標記爲swift。 OP可能知道如何將它翻譯成swift,但是你在Obj-C中回答,所以未來的求職者點擊標題爲「用_Swift_獲取ObjectIDs」的標題不會有與OP相同的結論 – soulshined 2015-02-06 17:23:51

1

我不認爲解析提供了一種方式來獲得所有OBJECTID在一個陣列。或者你可以遍歷每個對象,並檢索objectId的:

var objectIds = [String]() 

    let query = PFQuery(className:"TableName") 
    query.findObjectsInBackgroundWithBlock {(objects: [PFObject]?, error: NSError?) -> Void in 

     if error == nil { 
      if let objects = objects { 
       for object in objects { 
        objectIds.append(String(object.valueForKey("objectId")!)) 
       } 
      } 
     } else { 
      print("Error: \(error!) \(error!.userInfo)") 
     } 

     print(objectIds) 
    }