Swift for in循环:使用var get warning来使用let,使用let get error

Ian*_*son 15 swift

我在swift文件中有以下代码:

func testDictionary(dict :Dictionary<String,AnyObject>) {
    var str = ""
    for var key in dict.keys {
        str += key + ":" + dict[key]!.description + "\n"
    }
    self.alert("Dict", message: str)
}
Run Code Online (Sandbox Code Playgroud)

上面的代码varfor循环中对用户产生警告,即:

Variable 'key' was never mutated; consider changing to 'let' constant
Run Code Online (Sandbox Code Playgroud)

但是,当我更改为时var,let我收到以下错误:

'let' pattern cannot appear nested in an already immutable context
Run Code Online (Sandbox Code Playgroud)

当建议的更正是编译器错误时,为什么会收到警告?

ped*_*uan 26

声明中既不 需要var 也不 需要let.输入:

for key in dict.keys {
    str += key + ":" + dict[key]!.description + "\n"
}
Run Code Online (Sandbox Code Playgroud)