0

I've been pulling my hair out for a couple of days, and could really use some help.

I've got a property defined like this:

@objcMembers class Account: Object {

    dynamic var name = ""

When I load .name with a literal, i.e.,

            let newAccount = Account()
//            newAccount.name = self.accountTextField.text!
            newAccount.name = "John Brown"
            newAccount.isCurrent = true
            newAccount.highWaterBalance = 0.0

I get this:

acct.name is John Brown
acct.highWaterBalance is 0.0
acct.isCurrent is true

However, when I type "John Brown" into self.accountTextField, and use this code:

            let newAccount = Account()
            newAccount.name = self.accountTextField.text!
//            newAccount.name = "John Brown"
            newAccount.isCurrent = true
            newAccount.highWaterBalance = 0.0

I get this:

acct.name is 
acct.highWaterBalance is 0.0
acct.isCurrent is true

I've searched all over SO, and thought I had a fix in @rmaddy's contribution to this question.

I thought I was doing it right, but I'm still getting a blank from the textField.

Any help would be greatly appreciated!

Edit 1:

This code:

        print("Johnny B Goode\n")
        print ("\(self.accountTextField.text!)")
        print("Johnny B Bad\n")

Prints this result:

Johnny B Goode


Johnny B Bad

Edit 2:

For full clarity, and maybe I should have mentioned this before, here is the IBAction code that produces the alert in which self.accountTextField.text is located:

   @IBAction func addAccountButton(_ sender: Any) {

        // Pop alert to add new account



        let addAcctAlert = UIAlertController(title: "Create New Account", message: "Please name your new account", preferredStyle: UIAlertControllerStyle.alert)

        addAcctAlert.addTextField { (accountTextField) in
            accountTextField.placeholder = "Enter new account name"
            accountTextField.keyboardType = .`default`
        }

        addAcctAlert.addAction(UIAlertAction(title: "Continue", style: UIAlertActionStyle.destructive, handler: { action in

            // Create new Account with name, balance, isCurrent, save to disk
            print("Johnny B Goode\n")
            print ("\(self.accountTextField.text!)")
            print("Johnny B Bad\n")

            let newAccount = Account()
            newAccount.name = self.accountTextField.text!
//            newAccount.name = "John Brown"
            newAccount.isCurrent = true
            newAccount.highWaterBalance = 0.0

            self.addCreatedAccount(thisAccount: newAccount)
        }
        ))

        addAcctAlert.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.cancel, handler: nil))
        self.present(addAcctAlert, animated: true, completion: nil)
    }

It really does appear that regardless of what I type in the textField, it produces nothing. Is that possible?

Rakesha Shastri
  • 11,053
  • 3
  • 37
  • 50
rattletrap99
  • 1,469
  • 2
  • 17
  • 36

2 Answers2

0

If you are trying to fetch the value from account number text field you have added to your UIAlertController, then you should do it like this.

newAccount.name = addAcctAlert.textFields?.first?.text ?? "I haven't typed anything in alert controller's text field"

or

if let accountName: String = addAcctAlert.textFields?.first?.text {
    newAccount.name = accountName
}
sinner
  • 483
  • 3
  • 14
0

OK, thanks to the generous guidance of @Rakesha Shastri, @sinner and @MadProgrammer, I was led to the second answer in this question, which--with a couple of tiny tweaks--serves me perfectly!

Many thanks to @Johnykutty and @Andrey Gordeev for providing a succinct solution!

rattletrap99
  • 1,469
  • 2
  • 17
  • 36