2016-07-08 27 views
2

我有一個圖像數組,我想上傳它們在一個類中解析,但在一個指向另一個類的單獨的行。斯威夫特2保存多個圖像解析

所以我的代碼結構是這樣的

首先,我成功地保存對象和success內我得到的對象的對象ID。然後我想用指針將這些圖像保存到我之前在列和文件中逐個獲取的對象ID。一個代碼示例

//First object that i save to class "Point" 

    obj["user"] = PFUser.currentUser() 
    obj["title"] = "test" 
    obj.saveInBackgroundWithBlock { (success, error) -> Void in 
       if error == nil { 
       let ojId = self.obj.objectId //this is the object id of the already saved object. 


//and now im trying to save to another Class (named Gallery) the images with a pointer ojId. 

for i in 0...self.myArray.count-1 { 
let imageData = self.myArray[i].mediumQualityJPEGNSData 
        let imageFile = PFFile(name: "image.JPEG", data: imageData) 
        self.galleryImages["images"] = imageFile 
        self.galleryImages["point"] = PFObject(outDataWithClassName: "Point", objectId: "\(ojId!)") 
        self.galleryImages.saveInBackgroundWithBlock { (success, error) -> Void in 
        if error == nil { 
         print("saved") 
         let ojIdG = self.galleryImages.objectId 
         print(ojIdG!) 

        } 
       } 

有了它從陣列僅保存最後的圖像,儘管它得到這個多次作爲計數內部上面的代碼。

任何想法爲什麼會發生這種情況?

UPDATE。它不會在每次保存圖像時創建一個新行。它用下一個圖像替換該行。

+0

我相信它不能改變行!就像覆蓋它們一樣。 –

回答

2

因爲這些代碼將覆蓋你有具體的行要解析創建另一個行,如果是這樣的:

// this is what you add right after the "for" 

     let galleryImages = PFObject(className: yourClassName) 

    // then your code will remain the same! 

     let imageData = self.myArray[i].mediumQualityJPEGNSData..... 

希望它能幫助!