-4

I've been searching stackoverflow and would like to know what the best practice is to unwrap the textfield.text optional without force unwrapping. It works doing this, but I've been told to never force unwrap. But when I assign textfield.text to something, I'm only just passing the optional string. Can someone explain what would be best for this? Thanks!

Edit:

    func configuredMailComposeViewController() -> MFMailComposeViewController {

    let passTextField = TextField.text

    let mailComposerVC = MFMailComposeViewController()
    mailComposerVC.mailComposeDelegate = self 

    mailComposerVC.setToRecipients(["email@email.com"])
    mailComposerVC.setSubject(passTextField!)
    mailComposerVC.setMessageBody("Some Text", isHTML: false)

    return mailComposerVC
}

I'm trying to pass a textfield text and pass it to the email subject. The force unwrap doesn't cause an error, but I would like to know what the best practice is to pass this string

Edit 2:

 func configuredMailComposeViewController() -> MFMailComposeViewController {

    let mailComposerVC = MFMailComposeViewController()
    mailComposerVC.mailComposeDelegate = self 

    mailComposerVC.setToRecipients(["email@email.com"])
        if let passTextField = textfield.text {
           mailComposerVC.setSubject(passTextField)
         }
    mailComposerVC.setMessageBody("Some Text", isHTML: false)

    return mailComposerVC
}
  • 4
    Search on "optional binding" (`if let`). Read the [Optionals](https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/TheBasics.html#//apple_ref/doc/uid/TP40014097-CH5-ID309) section in the Swift book. In fact, you should read the entire book. – rmaddy Mar 30 '17 at 16:23
  • I second that notion. Read the language guide. – Alexander Mar 30 '17 at 16:35
  • I suppose this is what I am not understanding. I compose | if let someVariable = textfield.text {//statements} | the statement I'm trying to make is assigning someVariable the string. Yet, when I reiterate that in the statements section, I get an error. Or, if I leave the statements section blank, it won't recognize the identifier when I want to use it. – Hubert Mane Mar 30 '17 at 16:36
  • @HubertMane Please [edit] your question to include (as text, not a picture) actual code you are trying to use and clearly indicate exact error messages and where they happen. – rmaddy Mar 30 '17 at 16:40
  • I edited the question with an example I was working on. – Hubert Mane Mar 30 '17 at 16:50
  • Where's your attempt to use `if let` as described in your comment? – rmaddy Mar 30 '17 at 16:50
  • OK, your 2nd edit seems fine. What issue are you having with it? – rmaddy Mar 30 '17 at 16:55
  • I omitted because I was just showing what I was trying to do and asking for best practice, here was the attempt that I tried. – Hubert Mane Mar 30 '17 at 16:56
  • 1
    Your second edit is correct. But your earlier comment claimed you were getting errors. Since your second edit is the proper approach, what's the point of your question? – rmaddy Mar 30 '17 at 16:57

1 Answers1

-2

you can always unwrap a variable if you know for sure that it's already been defined, and you may need to take different action if the field has not been defined

var someVariableWithWideScope : String = ""

if myTextField.text != nil {
    someVariableWithWideScope = myTextField.text!
} else {
    // do something specific for nil values
}

of course, if all you're going to do for nil is assign a default value, you can do it one step as discussed elsewhere

let someVariable = myTextField.text ?? "default"

but that approach doesn't let you change execution flow

Russell
  • 5,436
  • 2
  • 19
  • 27
  • This is still doing a force unwrapping. Why not put all of that code in one line: `let someVariableWithWideScope = myTextField.text ?? ""` – rmaddy Mar 30 '17 at 16:56
  • Only because sometimes you want to take a different action if the value is still nil – Russell Mar 30 '17 at 16:58