12

I'm new to iOS development. I'm practicing linking actions/outlets between the View and Controller. My problem is that I don't know why it shows me some errors. Could you please take a look at my code and tell me what I'm missing?

I receive the following errors:

Value of optional type 'String?' must be unwrapped to a value of type 'String'

Coalesce using '??' to provide a default when the optional value contains 'nil'

Force-unwrap using '!' to abort execution if the optional value contains 'nil'

Here's the code:

@IBAction func submitAppointmentDetails(_ sender: Any) {
    appointmentSetName.text = "Congratulations," + nameField.text + "You have now set up an appointment with us!"
}

A photo of what's going on: https://i.stack.imgur.com/nRzQW.jpg

Edit: @matt This is a different question because the thread you thought this is a duplicate of doesn't have the solution that I was seeking, but two people in this thread did give me the solution. I never would have found the solution if I just read the thread that you shared.

Community
  • 1
  • 1
MoreSushiPlease
  • 131
  • 1
  • 1
  • 5
  • `nameField.text` is optional. – rmaddy Jun 03 '19 at 22:50
  • @rmaddy I'm trying to put the name the user has inputted in the text field through nameField.text. Even if I remove the .text and only leave nameField, it still shows errors. – MoreSushiPlease Jun 03 '19 at 22:53
  • @rmaddy Here's a photo: https://imgur.com/a/D8O3BzO – MoreSushiPlease Jun 03 '19 at 22:54
  • I think this is one of the rare scenarios where force unwrapping is totally justified. For some reason that I really don't understand, the `UITextField.text` API is imported into Swift as `String?` rather than `String`, even though it never returns `nil`. – Alexander Jun 03 '19 at 23:19

2 Answers2

26

nameField.text is of type String? meaning it is an optional string (it may contain a String or nil). Swift doesn't allow you to concatenate optional strings. You have to unwrap the value and you basically have the Swift's proposed ways you have put in the question:

  1. You can force unwrap the string nameField.text!. With this you tell Swift 'get the string value from that optional, I'm sure it's not nil', but this will crash the app if it's nil.

  2. You can use the coalesce operator ?? this way nameField.text ?? "<no_name>". This will do the following: if nameField.text is not nil, then the text will be displayed correctly; if nameField.text is nil, the "<no_name>" string will be displayed instead.

Vlad Rusu
  • 1,414
  • 12
  • 17
  • Hi! Adding ! to nameField.text did work in removing the errors, but when I ran the code, it didn't change "X" to "Congratulations, [name]. You have now set up an appointment with us!" It just changed to "C". Here is a photo: imgur.com/a/k7Eiuvd – MoreSushiPlease Jun 03 '19 at 23:08
  • Nevermind~ I just had to widen the label and set the lines to 0. Thank you! – MoreSushiPlease Jun 03 '19 at 23:42
  • Somehow your answer made me go in the right direction..Thanks! – coderBox Feb 08 '20 at 07:04
5

nameField.text gives an optional string so to concatenate it Replace

appointmentSetName.text = "Congratulations," + nameField.text + "You have now set up an appointment with us!"

with

appointmentSetName.text = "Congratulations," + nameField.text! + "You have now set up an appointment with us!"
Shehata Gamal
  • 98,760
  • 8
  • 65
  • 87
  • Ah! This did work in removing the errors, but when I ran the code, it didn't change X to "Congratulations, [name]. You have now set up an appointment with us!" It just changed to C. Here is a photo: https://imgur.com/a/k7Eiuvd – MoreSushiPlease Jun 03 '19 at 23:00
  • Nevermind~ I just had to widen the label and set the lines to 0. Thank you! – MoreSushiPlease Jun 03 '19 at 23:42