简体   繁体   中英

Xcode - Building App - Issue

I'm learning how Swift works and I'm stuck where the user will have to login with a username and password:

@IBAction func loginAction(_ sender: AnyObject) {

    var username = self.usernameField.text
    var password = self.passwordField.text

    if (username.utf16Count < 4 || password.utf16Count < 5) {

        var alert = UIAlertView(title: "Invalid", message: "Username must be greater then 4 and Password must be greater than 5.", delegate: self, cancelButtonTitle: "ok"  )
        alert.show()
    }else {
        self.actInd.startAnimating()
    }

This is what I've got, but Xcode is stating

if (username.utf16Count < 4 || password.utf16Count < 5)

is incorrect.

Can someone explain why it's wrong?

I'm using Xcode v8.0.

It should be:

  if (username?.utf16.count)!<4 || (password?.utf16.count)!<5{  }

Also the other thing you should do is to check if the username and password fields contain valid characters, for instance would you like users to be able to enter emojis/certain symbols/characters other than those in the English language, as part of their username/password?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM