迭代数组

时间:2017-03-10 23:27:15

标签: ios swift3 uibutton

我是swift的新手,并试图通过斯坦福的iOS 9课程来学习它。在从讲座#2(第7d部分)进行计算器分配时,我遇到了部分代码的问题,但我无法弄清楚原因。显然,我的代码的这个简单部分导致一元操作按钮无法工作,但对我来说似乎完全正确。

arrayOfDescription是我定义的字符串数组,它给出了一系列操作/数字,这些操作/数字通向显示中打印的值。我只是试图找到我的数组中按下的第一个操作按钮的索引(双转换是为了区分操作和数字),并根据变量operationLocation在数组中插入一些东西,如果" =&# 34;在一元操作符号之前出现在数组中,但代码在这里失败:

    case .UnaryOperation(let function):
            if arrayOfDescription[arrayOfDescription.count - 1] == "=" {
                   for item in arrayOfDescription {
                 while Double(item) != nil {
                        operationLocation += 1  }
                }
            }
            }

(这部分代码是模型的一部分,但插入部分位于控制器中:

        brain.arrayOfDescription.insert(mathematicalSymbol + "(", at: brain.operationLocation-1)

它不起作用。当我按下一元操作按钮时应用程序什么都不做,而它应该在标签上打印一些东西

更具体地说,案例是performOperation函数的一部分,几乎与教授写的一样。操作在字典中定义,该字典涵盖计算器中提供的所有常量和二进制/一元操作。这是完整的功能:

     func performOperation(symbol: String) {
    if let operation = operations[symbol] {
        switch operation {
        case .Constant(let value): accumulator = value
        case .UnaryOperation(let function):
            if arrayOfDescription[arrayOfDescription.count - 1] == "=" {
                   for item in arrayOfDescription {
                 while Double(item) != nil {
                        operationLocation += 1  }
                }
            }

            accumulator = function (accumulator)

        case .BinaryOperation(let function):
            isPartialResult = true
            executePendingBinaryOperation()
            pending = PendingBinaryOperationInfo(binaryFunction: function, firstOperand: accumulator)
        case .Equals :
            isPartialResult = false
            executePendingBinaryOperation()
        }
    }
    }
是的,我确定问题是我提到的第一部分,因为当我发表评论时,一切正常。此外,有很多细节,我不知道提到哪些(我可以写enum,字典,控制器代码等)所以我希望我写的内容足够有用。

0 个答案:

没有答案