分配数组中项的值会使条件绑定中的Bound值必须为Optional类型

Enc*_*PTL 5 swift

我收到编译错误说

Bound value in a conditional binding must be an Optional type
Run Code Online (Sandbox Code Playgroud)

下面是代码的屏幕截图

在此输入图像描述

Con*_*nor 1

您可以将 array[index] 的值转换为Optional,执行如下操作:

if let value = Int?(array[index]){
    result += value
}
Run Code Online (Sandbox Code Playgroud)

如果你的数组包含整数的话。您也可以使用 AnyObject?,但您会收到来自 xcode 的警告。