gpt4 book ai didi

swift - 二元运算符 '>=' 不能应用于类型 'String.IndexDistance?' (又名 'Optional' )和 'Int' 类型的操作数

转载 作者:行者123 更新时间:2023-11-30 11:55:30 25 4
gpt4 key购买 nike

在 Swift 4 中,我尝试将 UITextField 的文本长度与最小长度进行比较:

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

有人可以帮助我吗?

最佳答案

原因是 Equatable 可以与可选值一起使用,而 Comparable 则不能。您必须打开可选的包装。

一个合适且安全的解决方案是可选地绑定(bind) text 属性:

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

关于swift - 二元运算符 '>=' 不能应用于类型 'String.IndexDistance?' (又名 'Optional<Int>' )和 'Int' 类型的操作数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47866174/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com