Xcode complains about if-statement structure using Swift and Xcode 6

I was wondering what it is if the statement is not true. I use quickly. What I am trying to do is do a few checks from my text boxes, mainly to constrain their text lengths between the ranges declared in the if statement. Here is my code:

if countElements(usernameTextField.text) < 16 && 
   countElements(usernameTextField.text) > 4  && 
   countElements(passwordTextField.text) > 5  && 
   countElements(passwordTextField.text) < 16 {
   //Do something
} else {
   //Do something else
}

      

The compiler complains that it says the following:

"Enter" String! "does not conform to protocol '_CollectionType'"

Do you have any idea why this is showing up?

Thanks in advance for your advice / guidance / clarification.

Hooray!

+3


source to share


1 answer


text

propety is UITextField

declared as:

var text: String!

      

So, you should rewrite your code to:

Option 1:

if countElements(usernameTextField.text!) < 16 { ... }

      



This option is preferred as it will take into account the emoji character. For example, if you add a flag symbol that has 4 seats, you count as one.

Option 2 (don't use it):

if usernameTextField.text.utf16Count < 16 { ... }

      

These options take utf16 characters into account, so in the case of the emoji flag you will have a visible number of +3 characters. So don't go down this path. I only added this to show that it should not be used.

+2


source







All Articles