2

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.

9
  • did you connect the iboutlet of sqrimage from storyboard ? Commented Jan 23, 2019 at 14:34
  • That is because you called setSquareRec() before the views are loaded in the viewController. Commented Jan 23, 2019 at 14:37
  • Yes I did. @RatulSharker Commented Jan 23, 2019 at 14:37
  • @MarwenDoukh when are they loaded? I couldn't find the order of how swift compiles the code because there's no main function. As you notice I'm a noob at swift Commented Jan 23, 2019 at 14:38
  • @caddarina you are not implementing the delegate properly Commented Jan 23, 2019 at 14:43

2 Answers 2

1

You instantiate your view controller with let ARViewController = ViewController().

Try inflating it from a storyboard.

Feel free to ask if it isn't clear.

Sign up to request clarification or add additional context in comments.

1 Comment

I'm sorry, but it's not clear to me. What does inflating mean? I'm not a native speaker (and especially no professional programmer)
1

The way you are using your delegate is wrong infact you are not using the delegate itselg. Kindly look at the approach below.

import UIKit

class ViewController: UIViewController, SceneDelegate {

    @IBOutlet var squareImage: UIImageView!

    var scene = Scene()

    override func viewDidLoad() {
        super.viewDidLoad()

        //set delegate for scene as this delegate
        scene.sceneDelegate = self
    }

    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)
    }
}

protocol SceneDelegate{
    func setSquareRec()
}

class Scene: SKScene {

    var width = 0
    var height = 0
    var x = 0
    var y = 0
    var sceneDelegate: SceneDelegate?

    ...
    //call this delegate method like this
    //This will call the setSquareRec method to any class who is set as delegate
    sceneDelegate.setSquareRec()
    ...
}

not tested kindly test let me know incase of any issue

3 Comments

Thank you. I tried your code and it gave me the same error. I also tried another suggestion by adding self.loadView() into the first line of the function setSquareRec() additionally to your code. But it failed also...
please you share the code where you are loading the view controller
Okay I think there is the problem. I never initialize a ViewController except once in the class Scene (as you can see above) so I can use the setSquareRec function. A friend of mine gave me his code for an AR App. In his code he never initializes a ViewController and the App still worked.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.