Add Uitextfield text to an array

Asked

Viewed 40 times

-1

Oops, I know it may seem simple, but I’m having trouble picking up the typed text on Uitextfield and pressing the include button on an array, but I’m not getting it. Someone can help me?

 var listaDeNomes: [String]?
 @IBOutlet weak var inserirNomesTF: UITextField!
 @IBAction func IncludePlayer(_ sender: Any) {
    if inserirNomesTF.text == ""{
        print("pelo menos 5 caracteres")
    }else {
       listaDeNomes.append(inserirNomesTF.text)
    }
  }

If anyone can help me, I’d appreciate it

1 answer

3


The code has two problems:

  1. The array is not instantiated, you cannot add anything to it;
  2. You’re not handling options correctly.

Try something like this:

import UIKit

class ViewController: UIViewController {
    
    var listaDeNomes: [String]? = []
    
    @IBOutlet weak var inserirNomesTF: UITextField!
    
    @IBAction func IncludePlayer(_ sender: Any) {
        
        if inserirNomesTF.text!.count < 5{
            
            print("pelo menos 5 caracteres")
            
        }else {

            listaDeNomes!.append(inserirNomesTF.text!)
            
        }
      }
    }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.