Pass same data from one VC to another VC to another VC

124 Views Asked by At

I have a variable named name with string "Magellan" that needs to be added to each VC's nameLabel.

In first VC:

var name = "Magellan"

First VC prepare for segue:

destination?.nameLabel.text = name!
destination?.name = name!

In second VC:

var name = String()

Second VC prepare for segue:

destination?.nameLabel.text = name!
destination?.name = name!

In second VC to go to third VC:

Error: Unexpectedly found nil unwrapping nameLabel.text
Error: Unexpectedly found nil unwrapping name
1

There are 1 best solutions below

1
Aira Samson On

you have to change your

var name = String()

to

var name : String = “”

the difference of this is when you used the var name = String() you are assigning String class to your variable name while for var name : String = “” you are assigning empty value to your string type variable name.