【问题标题】:How to remove object from array in Parse with Swift 2.0如何使用 Swift 2.0 从 Parse 中的数组中删除对象
【发布时间】:2015-09-24 04:56:39
【问题描述】:

我在 Parse 的一列中有一组项目。

我可以使用代码获取该数组:

let query:PFQuery = PFQuery(className: "Names")

    query.whereKey("date", greaterThan: NSDate())
    query.findObjectsInBackgroundWithBlock {
        (object, error) -> Void in
        if object != nil
        {
            if(object!.count != 0)
            {
                for messageObject in object! {
                   self.arrayNames = ((messageObject as! PFObject)["arrayNames"] as? [String])!
                }
            } else {
                 print("No Objects")
            }
         } 
     }  // self.arrayNames = ["Aruna", "Bala", "Chitra", "Divya"]

在此我想删除一个项目并再次将其保存到解析中。

我可以在本地将其删除为 removeAtIndex,但如何从 Parse 中删除它?

【问题讨论】:

    标签: arrays swift parse-platform swift2


    【解决方案1】:

    在您检索到您的数组后,删除您不想要的一项,将新数组重新分配给检索到的对象,然后保存。

    【讨论】:

      【解决方案2】:

      在名字中,如果我想删除 Aruna,

      self.arrayNames.removeAtIndex(0)
      

      然后在 Parse 中更新这个数组,

      let query:PFQuery = PFQuery(className: "Names")
      
      query.whereKey("date", greaterThan: NSDate())
      query.findObjectsInBackgroundWithBlock {
          (object, error) -> Void in
          if object != nil {
              object!.setValue(self.arrayNames, forKey: "arrayNames")
              object!.saveInBackgroundWithBlock {
                    (success: Bool, error: NSError?) -> Void in
                    if(success) {
                        print("Success")                  
                    } else {
                        print("Error")
                    }
              }
          }
      }
      

      我发现,我不能直接从 Parse 的列中的数组中删除单个元素。所以我用我的本地数组更新那个数组。有效!

      【讨论】:

        【解决方案3】:

        如果您阅读文档,您会发现 one of the delete methods 很有用,例如 deleteInBackgroundWithBlock:

        object.deleteInBackgroundWithBlock { (succeeded: Bool, error: NSError?) -> Void in
            // do whatever you need to do
        }
        

        【讨论】:

        • 我只想在 Parse 的列中删除数组中的一项。 deleteInBackgroundWithBlock 将完全删除列中的数组。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-04-13
        • 2021-08-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多