我收到错误:
类型'数组字符的不可变值>'只有名称为removeAtIndex()
的变异成员数组应该包含内容,因为removeAtIndex行处于循环中,如果count> gt,那么条件是什么? 1
func evaluatePostFix(expression:Array<Character>) -> Character
{
var stack:Array<Character> = []
var count = -1 // Start at -1 to make up for 0 indexing
if expression.count == 0 {
return "X"
}
while expression.count > 1 {
if expression.count == 1 {
let answer = expression[0]
return answer
}
var expressionTokenAsString:String = String(expression[0])
if let number = expressionTokenAsString.toInt() {
stack.append(expression[0])
expression.removeAtIndex(0)
count++
} else { // Capture token, remove lefthand and righthand, solve, push result
var token = expression(count + 1)
var rightHand = stack(count)
var leftHand = stack(count - 1)
stack.removeAtIndex(count)
stack.removeAtIndex(count - 1)
stack.append(evaluateSubExpression(leftHand, rightHand, token))
}
}
}
任何人都知道为什么会这样?谢谢!
答案 0 :(得分:1)
因为所有函数参数都被值隐式传递为&#34; let&#34;,因此在函数内是恒定的,无论它们在函数外部是什么。
要修改函数中的值(不会影响返回值),可以显式使用var:
func evaluatePostFix(var expression:Array<Character>) -> Character {
...
}