I can't see any way to copy an NSView and create an identical NSView object. I see google hits about "use an NSData" but I don't understand that.
To straight up "copy" an NSView, the view has to implement the NSCopying
protocol. Unfortunately, NSView does not.
Fortunately, it does implement the NSCoding
protocol, which means we can still duplicate a view like:
NSData * archivedView = [NSKeyedArchiver archivedDataWithRootObject:myView];
NSView * myViewCopy = [NSKeyedUnarchiver unarchiveObjectWithData:archivedView];
And voilá! You now have a duplicate of myView
.
Edit: (Swift version)
let archivedView = NSKeyedArchiver.archivedData(withRootObject: myView)
let myViewCopy = NSKeyedUnarchiver.unarchiveObject(with: archivedView)
(archivedView
is of type Data
, not NSData
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With