title: how to unarchive plists in bundle using a path in Swift 5
body: <p>Short version:
  How to access an archived file in the bundle for unarchiving with <code>unarchivedObject(ofClass:from:)</code>. The core issue is going from a path (string or URL) to <code>Data</code> which is required by the method.</p>
  <p>Long version:
  I have many plists containing SCNNode hierarchies stored as plists in bundle resources.</p>
  <p><a href="https://i.stack.imgur.com/1tIve.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/1tIve.png" alt="enter image description here" /></a>
  I was able to access them in Swift 3 with:</p>
  <pre><code>let fileName = MoleName.DNA_ideal_comps     // retrieves String
  let filePath = getFilePath(fileName)
  
  guard let components = NSKeyedUnarchiver.unarchiveObject(withFile: filePath) as? [SCNNode] , components.count &gt;= 4
         else {  // no value for this key, or not enough nodes
              print(&quot;Couldn't find molecule (DNA_ideal_comps)&quot;)
                  return
          }  //  [ atoms_A, bonds_A, atoms_B, bonds_B ]
  
          scaleNode_2 = components[0]     // DNA A
          fixedNode_2 = components[1]     //   bonds
          scaleNode_3 = components[2]     // DNA B
          fixedNode_3 = components[3]     //   bonds
  
      
  // where filePath is:
  func getFilePath(_ fileName: String) -&gt; String { 
          if let path = Bundle.main.path(forResource: fileName, ofType: &quot;plist&quot;) {
                   return path
          }
          return &quot;path could not be found&quot;
    }
  </code></pre>
  <p>Here <code>fileName</code> is the name of the <code>plist</code> to retrieve, &quot;DNA_ideal_comps&quot; in this example. I created these independently of the active program due to the sheer volume of data; some of these plists contain over 30,000 items and there are about 90 total.</p>
  <p>The above attempt to unarchive is deprecated and I've struggled to replace it. My best try so far:</p>
  <pre><code>        guard let components = NSKeyedUnarchiver.unarchivedObject(ofClass: SCNNode.self, from: moleData), components.count &gt;= 4
  </code></pre>
  <p>But this fails with <em>Cannot convert value of type 'String' to expected argument type 'Data'</em>. Unlike the method in Swift 3, there appears no way here to use the path of the object to retrieve it.</p>
  <p>Is there a way to access these plists using this method? Is there another method more appropriate?</p>