12

Just by adding an NSCollectionView to Storyboard, the code won't compile with the error: Unknown segue relationship: Prototype.

Is this a known bug of Swift/XCode6-beta4 or am I missing something?

4 Answers4

7

This is a known bug, and is mentioned (with a workaround) in the release notes of Xcode 6 beta-5:

A storyboard may fail to compile after adding an NSCollectionView to it. (17009377)!

Workaround: Pick a xib that includes the NSCollectionView and load it into a Storyboard based View.!

Community
  • 1
  • 1
Damiaan Dufaux
  • 4,427
  • 1
  • 22
  • 33
  • Thank you, I didn't read that. Hopefully it will get definitely fixed in the next beta. –  Aug 07 '14 at 14:50
6

Update for Xcode 7 beta 6 (see release notes):

Interface Builder supports the new NSCollectionView API in 10.11, using dataSource and layouts like on iOS. (18997303)

  • Workaround: It is not possible to configure prototype items in Interface Builder. Use -[NSCollectionView registerClass:forItemWithIdentifier:] or -[NSCollectionView registerNib:forItemWithIdentifier:] in code.

Has anyone tried to work with it before? Should I just add an empty collection view to my story board, then create a nib file just for the collectionviewitem and finally link it via code?


Update for Xcode 7.1 beta 3

I've downloaded and tested the new beta today and the problem is still there. However they have strangely removed the workaround notes from the release notes as if they had fixed it...

gbdavid
  • 1,639
  • 18
  • 40
5

There's actually no need to create an extra XIB file when you are already using storyboards.

  1. Remove the connection between the NSCollectionView and the NSCollectionViewItem

  2. Give the view item an identifier

  3. Set the item prototype programmatically in your surrounding NSViewController:

@IBOutlet weak var collectionView: NSCollectionView!

override func viewDidLoad() {
    self.collectionView.itemPrototype = self.storyboard?.instantiateControllerWithIdentifier("collectionViewItem")
        as NSCollectionViewItem
}

This keeps all UI views in one place and thus provides a nicer workaround than having an alien XIB file hanging around.

Lars Blumberg
  • 19,326
  • 11
  • 90
  • 127
  • Amazing! Thank you for that. It's so weird that this works (it does, confirmed) given the method name that involves the word "Controller" (how is that item a controller? would have expected that method to only return objects of type NSViewController for instance, if I was to guess). – mz2 May 07 '16 at 18:52
  • Ah, nevermind, I spoke too soon, didn't notice an assertion failure. The documentation now suggests not setting the item prototype property anymore actually (when using the 10.11 SDK anyway). – mz2 May 07 '16 at 19:36
  • Right. When you're targeting earlier OS X versions before 10.11 this is however still a nicer workaround instead of creating an extra XIB file. – Lars Blumberg May 08 '16 at 11:17
3

In case you are using the OSX 10.11 SDK, the following info from Apple's release notes for it instructs not populating the itemPrototype property at all anymore (that of course assumes your minimum deployment target is 10.11):

To use NSCollectionView’s new API model, you specify a layout by setting an NSCollectionView’s “collectionViewLayout” property, and either provide a “dataSource” or bind your CollectionView’s “content” to an NSArray or NSArrayController (see “Binding Content to an NSCollectionView”, below). You must also disconnect and discard the NSCollectionView’s “itemPrototype”, which is a vestige of the 10.10-and-earlier API model.

mz2
  • 4,672
  • 1
  • 27
  • 47