Cannot get string variables from textfield to pass

2019-12-16 16:53发布

问题:

I have an app that allows the user to input their contact information and it sends the mail to my email address with the information they put into the textfields. Everything seems to work and I can send the mail with the desired text, however I can't seem to input the information from the textfield. Please help! Im kind of new to this :)

var name: String?
@IBOutlet weak var nameField: UITextField!

var contact: String?
@IBOutlet weak var contactField: UITextField!

var other: String = "nothing"
@IBOutlet weak var otherField: UITextField!


@IBAction func sendEmail(_ sender: Any) {

    let name = nameField.text

    let contact = contactField.text

    if other == nil {

    }else{
        let other = otherField.text!
    }

When I click the button, it pulls up the mail app with information in the body. Here is the code:

func configureMailController() -> MFMailComposeViewController {
    let mailComposerVC = MFMailComposeViewController()
    mailComposerVC.mailComposeDelegate = self

    mailComposerVC.setToRecipients(["email@gmail.com"])
    mailComposerVC.setSubject("Contact information")
    mailComposerVC.setMessageBody("Another Friend! \nMy name or business is: \(name) \nMy contact information is: \(contact) \nMy additional information includes: \(other)", isHTML: false)

    return mailComposerVC
}

Here is the output in the body of the mail app (even when I write things in the textfield):

"Another Friend!

My name or business is: nil

My contact information is: nil

My additional information includes: nothing"

EDIT:

Ive change my sendMail function to simply contain the outlet names:

func configureMailController() -> MFMailComposeViewController {
    let mailComposerVC = MFMailComposeViewController()
    mailComposerVC.mailComposeDelegate = self

    mailComposerVC.setToRecipients(["danielgannage@gmail.com"])
    mailComposerVC.setSubject("Staples Day")
    mailComposerVC.setMessageBody("Another Friend! \nMy name or business is: \(nameField.text) \nMy contact information is: \(contactField.text) \nMy additional information includes: \(otherField.text)", isHTML: false)

    return mailComposerVC
}

And now my output has textfield info:

"Another Friend!

My name or business is: Optional("whatever I put in the textfield")

My contact information is: Optional("whatever I put in the textfield")

My additional information includes: Optional("whatever I put in the textfield")"

But how do I get rid of the: Optional("") surrounding my string?

回答1:

Learning how to handle optional variables is an essential part of learning Swift. There are several methods to do what you are trying to do. If you are okay if some of these fields being unanswered, you could use a nil coalescing operator to set a default value. For example:

let name = nameField.text ?? "unknown"

Alternately, if it is no good without an answer, you could guard against that case:

guard let contact = contactField.text else { 
    // display missing info error 
    return
}

This would end the function, and not call the email client.



回答2:

just add ! at the end like let contact = contactField.text!