Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSCollectionView, Swift 4 - Cannot invoke "register"

Can anyone suggest how to solve this NSCollectionView issue? Converted from Swift 3 to Swift 4 and magic started happening :)

Code:

let item = NSNib(nibNamed: NSNib.Name(rawValue: "MACollectionViewItem"), bundle: nil)
collectionView.register(item, forItemWithIdentifier: "CollectionViewItem")

Error for the second line:

Cannot invoke 'register' with an argument list of type '(NSNib?, forItemWithIdentifier: String)'

like image 458
spacecash21 Avatar asked Dec 08 '22 16:12

spacecash21


1 Answers

In Swift 4, you need to use NSUserInterfaceItemIdentifier instead of a String to identify a user interface element.

You should define static constants for identifiers and reference them when registering nibs.

Example:

extension NSUserInterfaceItemIdentifier {
    static let collectionViewItem = NSUserInterfaceItemIdentifier("CollectionViewItem")
}

collectionView.register(item, forItemWithIdentifier: .collectionViewItem)
like image 141
Tamás Sengel Avatar answered Dec 27 '22 03:12

Tamás Sengel