0

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
jww
  • 97,681
  • 90
  • 411
  • 885
magellan
  • 63
  • 7

1 Answers1

-1

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.

Dharmesh Mansata
  • 4,422
  • 1
  • 27
  • 33
Aira Samson
  • 226
  • 2
  • 10
  • Check this [Swift string via string literal vs initializer](https://stackoverflow.com/questions/31197008/swift-string-via-string-literal-vs-initializer) – RajeshKumar R Nov 20 '19 at 05:16