// // KMReusable.swift // PDF Reader Pro // // Created by tangchao on 2023/12/11. // import Foundation public protocol KMReusable: AnyObject { static var identifier: String { get } static var itemIdentifier: NSUserInterfaceItemIdentifier { get } } extension KMReusable { public static var identifier: String { return String(describing: Self.self) } public static var itemIdentifier: NSUserInterfaceItemIdentifier { return NSUserInterfaceItemIdentifier(rawValue: self.identifier) } } extension KM where Base: NSCollectionView { public func register(cell: T.Type) where T: KMReusable { base.register(cell, forItemWithIdentifier: T.itemIdentifier) } public func dequeueReusableCell(for indexPath: IndexPath) -> T where T: KMReusable { guard let cell = base.makeItem(withIdentifier: T.itemIdentifier, for: indexPath) as? T else { fatalError("Could not dequeue reusable cell with identifier: \(T.itemIdentifier)") } return cell } }