title: Copying NSView using archivedDataWithRootObject / unarchiveObjectWithData
body: <p>I am trying to copy NSView and then add it to the same superview. I've found some code on Stackoverflow that uses archivedDataWithRootObject/unarchiveObjectWithData approach, but I am still missing something as the unarchived NSView doesn't show when added with an addSubview.</p>
  
  <p><a href="https://i.stack.imgur.com/HD4aM.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/HD4aM.png" alt="enter image description here"></a></p>
  
  <p>Here is the setup in Interface Builder. There is NSView with a referencing outlet in the ViewController named customView. It has two NSView subviews. I set their identities as "one" and "two. And extended NSView class so they could be found via identity. I am trying to copy the second NSView and then add it to the same parent NSView, but it doesn't appear.</p>
  
  <p>Here is the code:</p>
  
  <pre><code>import Cocoa
  
  class ViewController: NSViewController {
    @IBOutlet var customView: NSView!
  
    override func viewDidLoad() {
      super.viewDidLoad()
      let v = customView.viewWithID("two")
  
      // create an NSData object from myView
      let archive = NSKeyedArchiver.archivedDataWithRootObject(v!)
  
      // create a clone by unarchiving the NSData
      let myViewCopy = NSKeyedUnarchiver.unarchiveObjectWithData(archive) as! NSView
  
      // I am setting the background color to make it visible
      myViewCopy.backgroundColor = NSColor.redColor()
  
      // adding it
      customView.addSubview(myViewCopy)
      // .. and nothing happens!
      // so I tried to specify origin, to make sure it's not outside the visible area
      myViewCopy.frame.origin.x = 0
      myViewCopy.frame.origin.y = 0
      // .. still no effect
    }
  }
  
  extension NSView {
  var backgroundColor: NSColor? {
      get {
          if let colorRef = self.layer?.backgroundColor {
              return NSColor(CGColor: colorRef)
          } else {
              return nil
          }
      }
      set {
          self.wantsLayer = true
          self.layer?.backgroundColor = newValue?.CGColor
      }
  }
  
  func viewWithID(id:String) -&gt; NSView? {
      for view in subviews {
          if let identifier = view.identifier {
              if identifier == id {
                  return view
              }
          }
      }
      return nil
    }
  }
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/de7nU.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/de7nU.png" alt="enter image description here"></a></p>
  
  <p>What am I missing?</p>