0

(I am) new in the Xcode.

I am making this app, and first I had 3 viewcontrollers; each used to talk with each other with a button: from the first you click a button that goes to the second, and go on. The thing is: I put some switchs in the first viewcontroller and they execute a segue for the second viewcontroller, changing a label. The first viewcontroller has 2 buttons - one of them is part of the segue of the switchs, and the other is for the third viewcontroller. Now my problem: when I click the second button on the first viewcontroller it works just fine, but if click the swicth but not the button of the segue, instead the second button the app crashes.

this is the line of my segue:

if switch1?.isOn == true
              {
              let secondController = segue.destination as! SecondViewController
                secondController.myString1 = "blabla"; ()

As I said: it doesn't interfere in the normal use of the app, but if I click the switch, but not click the button for the segue, instead click the button for the third viewcontroller, the app crashes.

Could not cast value of type 'app2.ViewController' (0x103879548) to 'app2.SecondViewController' (0x1038793a0).
2020-02-25 20:43:39.507421+0000 app2[18433:249963] Could not cast value of type 'app2.ViewController' (0x103879548) to 'app2.SecondViewController' (0x1038793a0).

I need a way to write a condition that if other button is click other than the one of the segue, the switchs should be off.

Any help would be highly appreciated.

Rafa
  • 13
  • 1
  • 4

1 Answers1

0

This error message you're getting means that segue.destination is not of type SecondViewController and in fact it appears to be of type ViewController. More than likely what this means is you have to go into your Storyboard and more closely examine:

  1. Your segue to see if its destination is in fact of type SecondViewController
  2. The Custom Class part of the storyboard Identity Inspector to make sure you picked a Class and Module for the view controller in question

The above answer is all pretty much in this post, plus some more context/details

23r0c001
  • 151
  • 1
  • 6
  • Thanks for the help. I investigate your suggestion, but in the end what really worked was follow the advice from the red button that Xcode sent us every time some code is wrong and suggest a fix: let secondController = segue.destination as? SecondViewController secondController?.myString1 = "blabla"; () as I am new on that, I have no idea what it means, but it did work. – Rafa Feb 27 '20 at 11:26
  • Well done. And interesting! I would have expected the `?` operator to cause compile-time errors and the `!` to cause only runtime errors. Out of curiosity, and if you care to look into it, does it still produce the compiler error if you leave out the second question mark, thusly: `let secondController = segue.destination as? SecondViewController secondController.myString1 = "blabla"; ()` Also if you're curious [here](https://developer.apple.com/swift/blog/?id=23) is the Apple developer page on type-casting with `as`, `!`, and `?`: – 23r0c001 Feb 27 '20 at 20:33
  • Hello there. Yep, the error is there without the "?". Thanks for the tip of Apple page. – Rafa Feb 28 '20 at 11:21