ios - 无法使用类型为 'Double' 的参数列表调用类型为 '(String?)' 的初始值设定项

标签 ios swift xcode int

我有两个问题:

let amount:String? = amountTF.text
  1. amount?.characters.count <= 0

报错:

Binary operator '<=' cannot be applied to operands of type 'String.CharacterView.IndexDistance?' (aka 'Optional<Int>') and 'In
  1. let am = Double(amount)

报错:

Cannot invoke initializer for type 'Double' with an argument list of type '(String?)'

我不知道怎么解决。

最佳答案

amount?.count <= 0这里的金额是可选的。你必须确保它不是nil .

let amount:String? = amountTF.text
if let amountValue = amount, amountValue.count <= 0 {

}

amountValue.count <= 0只有在 amount 时才会被调用不为零。

同样的问题 let am = Double(amount) . amount是可选的。

if let amountValue = amount, let am = Double(amountValue) {
       // am  
}

关于ios - 无法使用类型为 'Double' 的参数列表调用类型为 '(String?)' 的初始值设定项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46989131/

相关文章:

iphone - 与 NSLocalizedString( @"Text aka. key", @"Description")中使用的键具有不同的后备语言

ios - 使 UIImageView 变圆

c# - 从子控件检测 ViewDidUnload

iphone - 为什么我的Xcode 4.6 本地化文件只能选择英文?

ios - 如何禁用 developerprofile 文件另一个 mac

ios - 安装新的 cocoapod 后缺少所需的模块

iphone - iPhone 上的 REST API 问题

ios - 使用 Swift 保存和加载高分

ios - 我可以在应用程序终止后从 NSUserDefaults 检索数据吗?

ios - 集合中项目的顺序已更改,Set,Swift