Home » Swift » What's the equivalent to loadNibNamed in Swift

What's the equivalent to loadNibNamed in Swift

Posted by: admin November 30, 2017 Leave a comment

Questions:

I am converting my Objective-C code to Swift and was wondering what is the swift equivalent to this code

[[[NSBundle mainBundle] loadNibNamed:@"Games-New" owner:nil  
options:nil] firstObject]; 

Thanks.

Answers:
NSBundle.mainBundle().loadNibNamed("Games-New", owner: nil, options: nil)[0]

Questions:
Answers:

Maybe the API has changed. The third parameter of loadNibNamed is no longer options, it’s topLevelObjects. The following code works for me:

var objects: NSArray?
NSBundle.mainBundle().loadNibNamed("MyView", owner: self, topLevelObjects: &objects)

Questions:
Answers:

Here is a UIView extension

public extension UIView {

    public class func instantiateFromNib<T: UIView>(viewType: T.Type) -> T {
        return NSBundle.mainBundle().loadNibNamed(String(describing: viewType), owner: nil, options: nil)?.first as! T
    }

    public class func instantiateFromNib() -> Self {
        return instantiateFromNib(self)
    }

}

Usage

let awesomeView = AwesomeView.instantiateFromNib()

OR

let awesomeView = UIView.instantiateFromNib(AwesomeView.self)

Questions:
Answers:

Use this to load your nib.

let myView = NSBundle.mainBundle().loadNibNamed("MyView", owner: nil, options: nil)[0] as UIView

Then you can add it to your view as you normally would.

view.addSubview(myView)

Questions:
Answers:

Swift 3


name
The name of the nib file, which need not include the .nib extension.

owner
The object to assign as the nib’s File’s Owner object.

options
A dictionary containing the options to use when opening the nib file.

first
if you do not define first then grabing all view .. so you need to grab one view inside that set frist .

Bundle.main.loadNibNamed("yourUIView", owner: self, options: nil)?.first as! yourUIView

Questions:
Answers:

For Swift 3:

Bundle.main.loadNibNamed("Games-New", owner: nil, options: nil)?[0]

FYI: NS is (not entirely) removed in Swift 3

Questions:
Answers:

In the docs, it looks like it is different for macOS and iOS.

For macOS and Swift 4, the following worked for me:

func loadFromNib(_ nibName: String) -> NSView? {
    var topLevelObjects: NSArray? = NSArray()
    if Bundle.main.loadNibNamed(NSNib.Name(rawValue: nibName), owner: self, topLevelObjects: &topLevelObjects) {
        let views = topLevelObjects?.filter { $0 is NSView }
        return views?[0] as? NSView
    } else {
        return nil
    }
}

Questions:
Answers:

This is how to do in macOS in Swift 4

Register

collectionView.register(NSNib(nibNamed: NSNib.Name(rawValue: "Cell"), bundle: nil)!,
                            forItemWithIdentifier: NSUserInterfaceItemIdentifier(rawValue: "Cell"))

Dequeue

func collectionView(_ collectionView: NSCollectionView, itemForRepresentedObjectAt indexPath: IndexPath) -> NSCollectionViewItem {

  let item = collectionView.makeItem(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: "Cell"), for: indexPath)
  guard let cell = item as? Cell else { return item }

  let value = values[(indexPath as NSIndexPath).item]
  cell.imageView?.setImage(url: value, placeholder: Image(named: NSImage.Name(rawValue: "placeholder")))

  return cell
 }