2017-08-02 46 views
0

我有一個名爲「type」的屬性,它可以是0或1。 我可以在文件夾中找到列表和文件夾,與查找器中相同。它由一對多的關係管理。Swift核心數據 - 如何更新所有子實體的屬性?

  • 類型0:「文件夾」。
  • 類型1:「列表」。

該實體還具有 「選擇」 命名的屬性,其可以是0,1或2。

  • 選擇0:沒有子選擇。
  • 選擇1:選擇文件夾內至少一個但不是全部的孩子。
  • 選擇2:選擇文件夾內的所有孩子。

當我標記(選擇)一個文件夾我想選擇裏面的所有子列表或文件夾。這可以通過檢查所有孩子(使用關係)和setValue(2, for key: "selected")來完成。

問題1:如何更新兒童孩子的屬性(文件夾內容,文件夾內,文件夾內......)?

問題2:如果取消選擇文件夾內的實體,如何取消選擇(將「selected」設置爲0或1)所有父列表和文件夾(直到根級別)?


編輯: datamodel

+0

請顯示您的數據模型。 – sschale

+0

我添加了數據模型。 – Tibo

回答

0

最後我設法在for循環檢查第一級子對象,並將它們添加到一個數組(childFolderA)來解決我的問題。

然後我做2個for環的while循環內:

  1. 第一for迴路識別所有子文件夾,並把它們添加到一個數組(childFolderB)。
  2. 然後在第二個for循環中掃描此數組,並將所有子對象添加到其他數組(childFolderA)。
  3. 它會繼續while childFolderA或childFolderB數組包含一些東西。

該代碼在函數中。

 // Find the list/folder object of the row selected 
     if let objs = controller.fetchedObjects , objs.count > 0 { 
      let item = objs[indexPath.row] 

     // Manage selection for selected object. 
     if item.selected == 0 || item.selected == 1 { 
      item.selected = 2 
     } else { 
      item.selected = 0 
     } 

     var childFolderA: [List] = [] 
     var childFolderB: [List] = [] 

     // Find the 1st level child lists 
     for child in item.child_lists! { 
      if item.selected == 0 || item.selected == 1 { 
       (child as AnyObject).setValue(0, forKey: "selected") 
      } else { 
       (child as AnyObject).setValue(2, forKey: "selected") 
      } 
      // Add all 1st level child to the array: childFolderA 
      childFolderA.append(child as! List) 
     } 

     // Find all child folders and lists 
     while (!childFolderA.isEmpty || !childFolderB.isEmpty) { 
      for child in childFolderA { 
       if child.type == 1 {            // Folder 
        for child2 in child.child_lists! { 
         if item.selected == 0 { 
          (child2 as AnyObject).setValue(0, forKey: "selected") // Set selected circle to 「All」 
         } else { 
          (child2 as AnyObject).setValue(2, forKey: "selected") // Set selected circle to 「0」 
         } 
         childFolderB.append(child2 as! List) 
        } 
       } else {               // List 
        if item.selected == 0 { 
         child.setValue(0, forKey: "selected")      // Set selected circle to 「one list」 
        } else { 
         child.setValue(1, forKey: "selected")      // Set selected circle to 「0」 
        } 
       } 
      } 
      childFolderA.removeAll()            // Reset childFolderA 

      for child in childFolderB { 
       if child.type == 1 {            // Folder 
        for child2 in child.child_lists! { 
         if item.selected == 0 { 
          (child2 as AnyObject).setValue(0, forKey: "selected") // Set selected circle to 「All」 
         } else { 
          (child2 as AnyObject).setValue(2, forKey: "selected") // Set selected circle to 「0」 
         } 
         childFolderA.append(child2 as! List) 
        } 

       } else {               // List 
        if item.selected == 0 { 
         child.setValue(0, forKey: "selected")      // Set selected circle to 「one list」 
        } else { 
         child.setValue(1, forKey: "selected")      // Set selected circle to 「0」 
        } 
       } 
      } 
      childFolderB.removeAll()            // Reset childFolderA 
     } 
     childFolderA.removeAll() 
     childFolderB.removeAll() 

它可能不是100%安全,所以讓我知道它是否可以改進。

相關問題