无法使用类型为((String?)'的参数列表调用类型为'Double'的初始化程序

编程入门 行业动态 更新时间:2024-10-28 17:29:09
本文介绍了无法使用类型为((String?)'的参数列表调用类型为'Double'的初始化程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有两个问题:

let amount:String? = amountTF.text

  • amount?.characters.count <= 0
  • 出现错误:

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

  • let am = Double(amount)
  • 出现错误:

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

    我不知道该怎么解决.

    推荐答案

    amount?.count <= 0,此处金额是可选的.您必须确保它不是nil.

    amount?.count <= 0 here amount is optional. You have to make sure it not nil.

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

    amountValue.count <= 0仅在amount不为零时被调用.

    amountValue.count <= 0 will only be called if amount is not nil.

    与此let am = Double(amount)相同的问题. amount是可选的.

    Same issue for this let am = Double(amount). amount is optional.

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

    更多推荐

    无法使用类型为((String?)'的参数列表调用类型为'Double'的初始化程序

    本文发布于:2023-11-26 16:32:55,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1634388.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:类型   初始化   参数   程序   列表

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!