0

Basically, all I want to do is get the textfields data, put it into an array and then check to see if the username/password is greater than or equal to 6. I'm completely lost as to why this is always going to else? The answer is probably obvious, but I'm stuck!

Code:

import UIKit

class SignUpVC: UIViewController {

//MARK: Outlets
@IBOutlet weak var usernameTextField: UITextField!
@IBOutlet weak var passwordTextField: UITextField!
@IBOutlet weak var showErrorLabel: UILabel!

//MARK: Actions
@IBAction func goBackToLoginButton(_ sender: Any) {

    let username: String = usernameTextField.text!
    let password: String = passwordTextField.text!

    //username/password content into an array
    let userNameArray = ["\(username)"]
    let passwordArray = ["\(password)"]

    //Checks to see if username & password is greater than or equal to 6
    if userNameArray.count >= 6 && passwordArray.count >= 6 {
        print("Username and password is greater than or equal to 6!")
    } else {
        print("Username and password is less than 6!")
        showErrorLabel.isHidden = false
    }

    //Dismiss old views
    dismiss(animated: true, completion: nil)
}

override func viewDidLoad() {
    super.viewDidLoad()
}
Adrian Sanguineti
  • 2,455
  • 1
  • 27
  • 29
Polo
  • 5
  • 3
  • 3
    You are checking the lengths of the arrays, and both arrays have exactly *one* element. – Martin R Nov 20 '16 at 21:33
  • Do you want to check whether the username or password is more than 6 characters? – Sweeper Nov 20 '16 at 21:42
  • Considering I spent a good hour or two looking around for my issue + changing the code trying to "debug" it. I wouldn't have asked if I wasn't truly stuck. Do apologise for my inconvenience. Thank you for the help. – Polo Nov 20 '16 at 21:46
  • @Sweeper All I wanted to do was - if the username or password was less than 6, then they will stay on the same view-controller, else if true move back to the login view-controller. – Polo Nov 20 '16 at 21:48
  • @Polo What help do you need? You create two new arrays with exactly one value each. Then the next line is an `if` statement that checks if those new arrays have 6 or more values. Clearly this can't be true. – rmaddy Nov 20 '16 at 21:48
  • What do you mean by "less than 6"? Is "abcd" less than 6? – Sweeper Nov 20 '16 at 21:50
  • @rmaddy I got the idea of an 'element' wrong. I thought that .count was getting the element in the array and then counting how many characters it contained. – Polo Nov 20 '16 at 21:52
  • @Polo You really need to clarify in your question that you are trying to check the number of characters entered into the two text fields. – rmaddy Nov 20 '16 at 21:54
  • I just got confused. I will make my explanation in more detail next time, if I ever need it. Thanks. – Polo Nov 20 '16 at 22:00

1 Answers1

2

As what @Martin R pointed out, both of your arrays consist of a single element hence .count returns 1 for this case.

Since you're trying to check to see if the password and username lengths are >= 6, then there's no need for the arrays at all. Just do this instead

let userName: String = usernameTextField.text!
let password: String = passwordTextField.text!

if userName.characters.count >= 6 && password.characters.count >= 6
{
  print("Username and password is greater than or equal to 6!")
}
else
{
   print("Username or password is less than 6!")
   showErrorLabel.isHidden = false
}
eshirima
  • 3,837
  • 5
  • 37
  • 61