Solved it.
As the comments pointed out, the problem was that I was not accessing ViewController correctly.
In order to access my ViewController outside of the ViewController class, I was creating a new instance of it by ViewController()
.
I solved it by putting the function inside the class and changing ViewController()
part to self.ViewController
.
This answer also helped me as well.
https://stackoverflow.com/a/45932084/7414387
solved Xcode unexpectedly found nil while unwrapping an Optional, but no idea why