I am trying to load a NIB from file.
I have this code, from here
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
let element = results[0] as? Self
return results[0] as? Self
}
}
results have two elements, a NSView and NSApplication.
The problem here is the element is nil. The commented code was also giving me nil there.
I am new to swift. What is this Self delivering or what it represents on the last line of createFromNib?