makeItemWithIdentifier returns 0x0

When the following OSX code is ran, item is 0x0000000000000000, whatsmore, I can see that my nib's coder init is being called as I put a break points there.


Why then do I get nothing back from this method?


func collectionView(collectionView: NSCollectionView, itemForRepresentedObjectAtIndexPath indexPath: NSIndexPath) -> NSCollectionViewItem
{
    let item = collectionView.makeItemWithIdentifier("MyNibId", forIndexPath: indexPath)
}


Kind Regards


Chris

Accepted Reply

First thing, make sure that you either:

  • Register the class
  • Or register the nib


Second thing, if you're using a nib file, you need to add the NSCollectionViewItem object to the scene and then make sure to set it to your subclass of NSCollectionViewItem.

Replies

Hi I've got the same problem here:


func collectionView(_ collectionView: NSCollectionView, itemForRepresentedObjectAt indexPath: IndexPath) -> NSCollectionViewItem 
{
   let item = collectionView.makeItem(withIdentifier: ColorPickerPopover.itemIdentifier, for: indexPath)
   if let newItem = item as? ColorCollectionViewItem {
   bla bla bla


Apparently, the makeItem function set item to "uninitialized"... as is saying the debugger (po item)

Even more strange:


func collectionView(_ collectionView: NSCollectionView, itemForRepresentedObjectAt indexPath: IndexPath) -> NSCollectionViewItem
{
   var item = MyCollectionViewItem()
   item = collectionView.makeItem(withIdentifier: "Identifier", for: indexPath)
   if let newItem = item as? MyCollectionViewItem {
   bla bla bla

then, item is instantiated on line 3... but line 4 brings it back to "uninitialized"...


Have you solve your problem since?

We may should report this as a bug?


Josh

Same problem here.


Did you find a way around the crash?

First thing, make sure that you either:

  • Register the class
  • Or register the nib


Second thing, if you're using a nib file, you need to add the NSCollectionViewItem object to the scene and then make sure to set it to your subclass of NSCollectionViewItem.

The second step worked for me, hours I spent on this :-(