Swift REPL 中的变量声明必须有初始值
Variable declaration in Swift REPL must have initial value
学习Swift闭包的语法声明。我遇到了问题:
let add: (Int, Int) -> Int
add = { (a: Int, b: Int) -> Int in
return a + b
}
错误:
variables currently must have an initial value when entered at the top level of the REPL
var add: (Int, Int) -> Int
Swift 没有没有赋值的单独变量声明(将 nil 赋值给可选值是最接近的),所以最简单的解决方法是:
let add: (Int, Int) -> Int = { (a: Int, b: Int) -> Int in
return a + b
}
正如评论者正确指出的和错误消息中指出的那样,我的声明仅在 Read-Eval-Print-Loop (REPL) 的顶层是正确的。
学习Swift闭包的语法声明。我遇到了问题:
let add: (Int, Int) -> Int
add = { (a: Int, b: Int) -> Int in
return a + b
}
错误:
variables currently must have an initial value when entered at the top level of the REPL var add: (Int, Int) -> Int
Swift 没有没有赋值的单独变量声明(将 nil 赋值给可选值是最接近的),所以最简单的解决方法是:
let add: (Int, Int) -> Int = { (a: Int, b: Int) -> Int in
return a + b
}
正如评论者正确指出的和错误消息中指出的那样,我的声明仅在 Read-Eval-Print-Loop (REPL) 的顶层是正确的。