Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

swift

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

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

but i'm getting the error

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


Ironically it works with

textFieldPassword.text?.count == 8

Can somebody help me?

like image 404
Josef Zoller Avatar asked Nov 25 '17 11:11

Josef Zoller


1 Answers

The reason is that Equatable works with optionals and Comparable does not. You have to unwrap the optional.

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

if let password = textFieldPassword.text, password.count >= 8 { ... }
like image 104
vadian Avatar answered Oct 28 '22 21:10

vadian