Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Variable 'xxx' was never mutated; consider changing to 'let' constant" ERROR

Tags:

xcode7

swift2

I have the following problem. I use this code below and I get the issue

"Variable 'characteristic' was never mutated; consider changing to 'let' constant"

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

When I change to "let", there's an Error:

'let' pattern cannot appear nested in an already immutable context

Why does it recommend me the change and afterwards mark it as an error?

like image 765
Luke Pistrol Avatar asked Jun 19 '15 09:06

Luke Pistrol


1 Answers

You just need to remove var, making your code:

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

because characteristic is immutable by default.

like image 172
ABakerSmith Avatar answered Sep 20 '22 17:09

ABakerSmith