.../Source/Extensions/NSObject+fromNib.swift
Line | Count | Source |
1 | | // |
2 | | // NSObject+fromNib.swift |
3 | | // CutBox |
4 | | // |
5 | | // Created by Jason Milkins on 25/3/18. |
6 | | // Copyright © 2018-2023 ocodo. All rights reserved. |
7 | | // |
8 | | |
9 | | import Cocoa |
10 | | |
11 | | extension NSObject { |
12 | | |
13 | 497 | class func fromNib<T>() -> T? { |
14 | 497 | var objectArray: NSArray? |
15 | 497 | let name = String(describing: T.self) |
16 | 497 | let bundle = Bundle(for: Self.self) |
17 | 497 | guard bundle.loadNibNamed( |
18 | 497 | name, |
19 | 497 | owner: nil, |
20 | 497 | topLevelObjects: &objectArray) else { |
21 | 1 | fatalError("Unable to load view from nib: \(name)") |
22 | 497 | } |
23 | 497 | |
24 | 740 | return objectArray?.first(where: { $0 is T }) as? T |
25 | 497 | } |
26 | | } |