【问题标题】:"Variable 'xxx' was never mutated; consider changing to 'let' constant" ERROR“变量 'xxx' 从未发生突变;考虑更改为 'let' 常量”错误
【发布时间】:2015-06-19 09:42:27
【问题描述】:

我有以下问题。我在下面使用此代码,我得到了问题

“变量 'characteristic' 从未发生变异;考虑更改为 '让'保持不变”

for var characteristic:CBCharacteristic in service.characteristics ?? [] {
    print(str)
    _selectedPeripheral!.writeValue(str.dataUsingEncoding(NSUTF8StringEncoding)!, forCharacteristic: characteristic, type: CBCharacteristicWriteType.WithoutResponse)
}

当我改为“让”时,出现错误:

'let' 模式不能嵌套在已经不可变的上下文中

为什么它建议我进行更改,然后将其标记为错误?

【问题讨论】:

    标签: swift2 xcode7


    【解决方案1】:

    你只需要删除var,制作你的代码:

    for characteristic in service.characteristics ?? [] {
        print(str)
        _selectedPeripheral!.writeValue(str.dataUsingEncoding(NSUTF8StringEncoding)!, forCharacteristic: characteristic, type: CBCharacteristicWriteType.WithoutResponse)
    }
    

    因为characteristic 默认情况下是不可变的。

    【讨论】:

    • 谢谢我试试这个:)
    • 感谢您的支持。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-01-03
    • 2018-08-18
    • 1970-01-01
    • 1970-01-01
    • 2020-04-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多