1234567891011121314151617181920212223242526272829 |
- //
- // NibLoadable.swift
- // PDF Master
- //
- // Created by Niehaoyu on 2022/10/25.
- //
- import Foundation
- protocol NibLoadable {
- static var nibName: String? { get }
- static func createFromNib(in bundle: Bundle) -> Self?
- }
- extension NibLoadable where Self: NSView {
- static var nibName: String? {
- return String(describing: Self.self)
- }
- static func createFromNib(in bundle: Bundle = Bundle.main) -> Self? {
- guard let nibName = nibName else { return nil }
- var topLevelArray: NSArray? = nil
- bundle.loadNibNamed(NSNib.Name(nibName), owner: self, topLevelObjects: &topLevelArray)
- guard let results = topLevelArray else { return nil }
- let views = Array<Any>(results).filter { $0 is Self }
- return views.last as? Self
- }
- }
|