I'm quite new to Swift and I'm struggling with this issue. I want to pass the size and point of squareImage to a different swift file in my project using setSquareRec.
View Controller:
class ViewController: UIViewController, SceneDelegate {
@IBOutlet var squareImage: UIImageView!
var scene = Scene()
func setSquareRec() {
scene.x = Int(squareImage.bounds.minX)
scene.y = Int(squareImage.bounds.minY)
scene.width = Int(squareImage.bounds.width)
scene.height = Int(squareImage.bounds.height)
}
...
}
The class:
protocol SceneDelegate{
func setSquareRec()
}
class Scene: SKScene {
var width = 0
var height = 0
var x = 0
var y = 0
...
let ARViewController = ViewController()
ARViewController.setSquareRec()
}
It gives me the error Thread 5: Fatal error: Unexpectedly found nil while unwrapping an Optional value in the first line ( scene.width = Int(sqareImage.bounds.minX) ) of the function setSquareRec
How is it possible that is has no value?! And how is it possible to pass it to another class? I looked at so many solutions but none of them worked or I don't get it.
setSquareRec()before the views are loaded in the viewController.