Adding a copy of an NSTabViewItem from an NSTabView into the same NSTabView

I’m creating a simple web browser and would like to implement tabs. For this purpose, I’m using an NSTabView. I basically want each tab to have a WebView that will show the website loaded. I’m starting with only one tab and want to add an exact copy of the first one when I create a new tab. I tried something like tabView.addTabViewItem(tabView.tabViewItemAtIndex(0).copy() as NSTableViewItem) but I’m getting an unrecognised selector sent to instance error. I’ve check the documentation for both NSTableView and NSTableViewItem but can’t figure out how to that.

EDIT
My whole error looks like this:

  • What's the difference between “bundle display name” and “bundle name” in cocoa application's info plist
  • Changing the background color of the unified NSToolbar (in Yosemite)
  • How do I add Main Menu to xib
  • How to deprecate a method in Xcode
  • How to embed an executable in my project
  • How to make a UIScrollView snap to icons (like App Store: Feature)
  • 015-03-14 17:15:57.884 Browser[1955:56547] -[NSTabViewItem copyWithZone:]: unrecognized selector sent to instance 0x600000100b40
    2015-03-14 17:15:57.884 Browser[1955:56547] -[NSTabViewItem copyWithZone:]: unrecognized selector sent to instance 0x600000100b40
    

    2 Solutions Collect From Internet About “Adding a copy of an NSTabViewItem from an NSTabView into the same NSTabView”

    Thanks to the answer on Copy NSView in cocoa and @MattyAyOh, I solved my problem by doing:

    var data = NSKeyedArchiver.archivedDataWithRootObject(view)
    var newView = NSKeyedUnarchiver.unarchiveObjectWithData(data) as NSView
    newTab.view = newView
    tabView.addTabViewItem(newTab)
    

    So you are getting that error because you are calling copy() on an object that doesn’t have -copyWithZone implemented. (Because NSTabViewItem subclasses NSView which doesn’t conform to the NSCopying protocol)

    What you can do to get around that is to initialize a new NSTabViewItem, then get the properties from the first item (tabViewItemAtIndex(0)), and then set them to your new NSTabViewItem

    from the documentation

    you can call setLabel, setIdentifier, setColor, setView, and setTooltip

    Once you’ve initialized and set your new NSTabViewItem, you can then add it to your NSTabView

    NSTabViewItem *tempTabViewItem = [NSTabViewItem new];
    //set your properties on tempTabViewItem here
    tabView.addTabViewItem(tempTabViewItem);