0

The documentation states that UINib will throw and exception if nibName is not found.

let nib = UINib(nibName: , bundle: )

Unfortunately, it's not marked with throws so I cannot capture the error.

Checking the .xib file exists using the following

FileManager.default.fileExists(atPath: )

The documentation for fileExists(atPath: ) highlights race conditions, file permissions and security. So the best way to move forward with file introspection is to just load the file and handle any erros loading the file rather than check it's location first.

So is there any way to load a nib without using the class UINib?

Jonesy
  • 195
  • 8
  • why you care if you already know that it shipped / not with package – Shehata Gamal Jul 03 '19 at 11:06
  • Why you want to check if it exists? Isn't exception an indication that it doesn't exist? – Kamran Jul 03 '19 at 11:15
  • 1
    I'm building a module. So I don't already know if it's with the package. Being a module, I would prefer to prevent crashes. – Jonesy Jul 03 '19 at 11:26
  • You can use `if let file = Bundle.main.loadNibNamed("XibName", owner: nil, options: nil)?.first { print(file)}` to load file. This will throw an exception if file doesn't exist. – Kamran Jul 03 '19 at 13:19

1 Answers1

0

You should use UINib, it will be a lot simpler. You just have to wrap it with some ObjcTry method like the solution presented in Swift Error Handling For Methods That Do Not Throw

ekscrypto
  • 3,718
  • 1
  • 24
  • 38