2017-11-25 92 views
5

斯威夫特4我想的也是UITextField的文本的長度具有最小長度比較:二元運算符'> ='不能應用於類型'String.IndexDistance?'的操作數(又名「可選<Int>」)和‘廉政’

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

但我得到的錯誤

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


具有諷刺意味的與

textFieldPassword.text?.count == 8 

有人可以幫助我的作品?

回答

13

原因是Equatable適用於可選項,而Comparable不適用。你必須打開可選的。

合適的安全解決方案是可選的綁定屬性text

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

感謝vadian,我會接受你的答案 –

相關問題