I have ported to Swift an existing ObjC code.
I have NSCollectionViewDataSource Methods which should be exactly doing the same :
In ObjC
- (NSCollectionViewItem *)collectionView:(NSCollectionView *)collectionView itemForRepresentedObjectAtIndexPath:(NSIndexPath *)indexPath {
// Message back to the collectionView, asking it to make a @"Slide" item associated with the given item indexPath. The collectionView will first check whether an NSNib or item Class has been registered with that name (via -registerNib:forItemWithIdentifier: or -registerClass:forItemWithIdentifier:). Failing that, the collectionView will search for a .nib file named "Slide". Since our .nib file is named "Slide.nib", no registration is necessary.
NSCollectionViewItem *item = [collectionView makeItemWithIdentifier:@"Slide" forIndexPath:indexPath];
AAPLImageFile *imageFile = [self imageFileAtIndexPath:indexPath];
item.representedObject = imageFile;
return item;
}In Swift
func collectionView(collectionView: NSCollectionView, itemForRepresentedObjectAtIndexPath indexPath: NSIndexPath) -> NSCollectionViewItem {
//
let item = collectionView.makeItemWithIdentifier("Slide", forIndexPath: indexPath)
let imageFile = self.imageFileAtIndexPath(indexPath)
item.representedObject = imageFile;
return item;
}I set a breakpoint at the return line. In ObjC, the slide.nib is displayed in the view, not in Swift.
I have checked all the nib files to make sure they are exactly identical in Swift and ObjC (notably the connections which define dataSource and delegates)
I do wonder why such a different behavior (in fact, nib never displayed in Swift)
item object appear different in ObjC and Swift. I wonder if it's just a different way to present in ObjC and Swift or a real problem.
ObjC (partial content)
item AAPLSlide * 0x618000120fa0
NSCollectionViewItem
NSViewController
NSResponder
_nibName id 0x0
_nibBundle NSBundle * @"/Users/me/Library/Developer/Xcode/DerivedData/CocoaSlideCollection ewnzbbfwoqqfsjgavhdwzvwhctyx/Build/Products/Debug/CocoaSlideCollection.app" 0x0000600000080af0
_representedObject AAPLImageFile * 0x61800008a230
_title id 0x0
view AAPLSlideCarrierView * 0x618000140f20
// ......
_collectionView NSCollectionView * 0x10050de20
NSView
_content id 0x0
_selectionIndexes NSMutableIndexSet * 0 indexes
_animationDuration double 0.25
_delegate AAPLBrowserWindowController * 0x6100000e0d00 n// in collectionView structure
Swift (partial content)
item NSCollectionViewItem 0x0000600000122300
_TtC20CocoaSlideCollection5Slide CocoaSlideCollection.Slide // aka bridge type ????
NSCollectionViewItem
NSViewController
NSResponder
_nibName NSIndexPath * 0x2000169
_nibBundle NSCollectionView * 0x10080fe80
NSView
_content id 0x0
// ....
_animationDuration double 0.25
_delegate CocoaSlideCollection.BrowserWindowController * 0x608000100120 // in nibBundle, which is also NSCollectionView
NSWindowController
NSResponder
_window NSWindow * 0x6100001e0200