The binary operator '> =' cannot be applied to operands of type String.IndexDistance? (aka 'Optional <Int>') and 'Int'

in Swift 4 I am trying to compare the length of a UITextField text with a minimum length:

if textFieldPassword.text?.count >= 8 { } 

but i get an error

 Binary operator '>=' cannot be applied to operands of type 'String.IndexDistance?' (aka 'Optional<Int>') and 'Int' 


Ironically, he works with

 textFieldPassword.text?.count == 8 

Can someone help me?

+5
source share
1 answer

The reason is that Equatable works with options, but Comparable does not. You must deploy optional.

A suitable and safe solution is to optionally bind the text property:

 if let password = textFieldPassword.text, password.count >= 8 { ... } 
+14
source

Source: https://habr.com/ru/post/1273649/


All Articles