0

I am new to Xcode and am wondering how I can fix an alert issue that I am having. My code is: localNotification.alertBody = "your person, \(lblUsername.text), is behind schedule for your meeting at \(lblScheduleDate.text)

What I get as an alert is: "your person, Optional("jessica"), is behind schedule for your meeting at Optional("July 25 | 1:30").

I'm wondering how to get rid of

  1. the Optionals
  2. the Quotations around the name and date/time
  3. the Parenthesis around the name and date/time.
Wyetro
  • 8,439
  • 9
  • 46
  • 64
jessica mele
  • 417
  • 5
  • 20
  • Try force unwrapping the variable with: lblUsername.text! and lblScheduleDate.text! – Jonas Jul 25 '16 at 19:01
  • for some reason it didn't translate properly in my question, I have it now as: localNotification.alertBody = "your helper, \(lblUsername.text), is behind schedule for you call at \(lblScheduleDate.text). Please stand by." So should it be: localNotification.alertBody = "your helper, lblUsername.text!, is behind schedule for you call at lblScheduleDate.text!. Please stand by." – jessica mele Jul 25 '16 at 19:04
  • Add an exclamation mark before both closing parentheses, after the 'text' – Jonas Jul 25 '16 at 19:07
  • localNotification.alertBody = "your helper, (lblUsername.text!), is behind schedule for you call at (lblScheduleDate.text!). Please stand by."? – jessica mele Jul 25 '16 at 19:10
  • Yes, that should work! If the parentheses persist, get rid of them around the variable names, I don't think you should need those. – Jonas Jul 25 '16 at 19:11
  • Thank you very much, i'll give it a try! – jessica mele Jul 25 '16 at 19:12
  • @jessicamele Please do not use force unwrapping with `!` like those users told you. This is unsafe and may lead to crashes later. Read http://stackoverflow.com/questions/32170456/what-does-fatal-error-unexpectedly-found-nil-while-unwrapping-an-optional-valu for good informations about this. – Eric Aya Jul 26 '16 at 09:34
  • @EricD - Then what would you suggest I do? – jessica mele Jul 26 '16 at 17:29
  • @jessicamele In your case, use `if let` (called "optional binding"). Explained [in the Swift guide](https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/TheBasics.html#//apple_ref/doc/uid/TP40014097-CH5-ID309). It's a very important aspect of Swift. :) – Eric Aya Jul 26 '16 at 17:35

2 Answers2

2

You can get rid of all three at the same time. All you have to do is unwrap the optionals. Whenever you print/show the text, add ! next to the variable. For example:

var username:String? = "Jessica"
print(username!)

Another way to accomplish this is to use an if-let statement:

if let user = username
{
    print(user)
}

Though it is much longer and inconvenient, this way is much safer and will prevent a

fatal error: unexpectedly found nil while unwrapping an Optional value

from occuring.

P.S. - Next time put your source code in your post so the question can be answered better. You will most likely be downvoted in the future.

Jack
  • 955
  • 1
  • 9
  • 30
  • thanks for the p.s. I'm new to posting on here too. how do you add it like that? – jessica mele Jul 25 '16 at 19:11
  • In the editor you'll see a two curly brackets. Press on them, then replace the 'code' with your code. – Jack Jul 25 '16 at 19:12
  • Don't do that. Force unwrapping with `!` leads to a crash when the value is nil. Use "optional binding" instead, for example with `if let`. Explained in the [Swift guide](https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/TheBasics.html#//apple_ref/doc/uid/TP40014097-CH5-ID309) and there's also an interesting read in [this canonical Q&A](http://stackoverflow.com/questions/32170456/what-does-fatal-error-unexpectedly-found-nil-while-unwrapping-an-optional-valu). – Eric Aya Jul 26 '16 at 17:45
  • `if let` works but the question was how to get rid of the Optional(""). This is the quickest and easiest way to do it if the programmer knows that the value is not nil. I will add your suggestion, though, to my answer. – Jack Jul 26 '16 at 17:46
  • `if let` is the basic answer for unwrapping an Optional. There's also `guard let`, optional chaining, and many other techniques. Force unwrapping with `!` (nicknamed "the crash operator" for a reason) is the *last* solution you want to use, and certainly not in any way the first and only tip you give to a beginner. :) – Eric Aya Jul 26 '16 at 17:54
  • 1
    Thanks for the advice! I never heard of `guard let` so I'll be sure to check it out! :) – Jack Jul 26 '16 at 17:56
2

Try force unwrapping the variable with: lblUsername.text! and lblScheduleDate.text!

Jonas
  • 1,473
  • 2
  • 13
  • 28
  • Don't do that. Force unwrapping with `!` leads to a crash when the value is nil. Use "optional binding" instead, for example with `if let`. Explained in the [Swift guide](https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/TheBasics.html#//apple_ref/doc/uid/TP40014097-CH5-ID309) and there's also an interesting read in [this canonical Q&A](http://stackoverflow.com/questions/32170456/what-does-fatal-error-unexpectedly-found-nil-while-unwrapping-an-optional-valu). – Eric Aya Jul 27 '16 at 11:16