ios - 二元运算符 '+' 不能应用于类型 'Double' 和 'Element' (又名 'AnyObject' )的操作数

标签 ios arrays xcode swift for-loop

let totalPrice: Double = price * value
var money: Double = 0
for totalPrice in dataArray {
    money = money + totalPrice
}

无法分配二元运算符“+”

最佳答案

如何声明“dataArray”?

它是[AnyObject]还是[Double]

如果是前者,您需要使用以下方法将其转换为 double 型:

guard let price = totalPrice as? Double else { continue }
money = money + totalPrice

关于ios - 二元运算符 '+' 不能应用于类型 'Double' 和 'Element' (又名 'AnyObject' )的操作数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34411048/

相关文章:

ios - 如何使用按钮创建自定义 Collection View ?

ios - UIView滑动过渡淡出

ios - 枚举元素不能作为实例成员引用

javascript - 当 if 条件多次满足时从数组返回多个值

xcode - OpenCV编译有关 “cvloadimage”的错误,在main.o中引用自:_main

ios - UILocationNotfication.fireDate 过去 - bug?

c# - 为什么 int 数组的最大大小小于 Int32.MaxValue?

ruby-on-rails - 如何将数组分解为字符串 MVC?

iphone - 如何在没有新版本的情况下更新应用程序商店中的应用程序?

ios - #pragma mark 未列出第一个组名