Swift 斯坦福大学斯威夫特课程的斯威夫特计算器
我们在斯坦福大学swift课程中使用计算器,在出现错误时调用方法performOperation(在声明前使用局部变量“performOperation”)。如有任何帮助或建议,我们将不胜感激。谢谢Swift 斯坦福大学斯威夫特课程的斯威夫特计算器,swift,Swift,我们在斯坦福大学swift课程中使用计算器,在出现错误时调用方法performOperation(在声明前使用局部变量“performOperation”)。如有任何帮助或建议,我们将不胜感激。谢谢 import UIKit class ViewController: UIViewController { @IBOutlet weak var display: UILabel! var userIsInTheMiddleOfTypingNumber: Bool = false @IB
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var display: UILabel!
var userIsInTheMiddleOfTypingNumber: Bool = false
@IBAction func appendDigit(sender: UIButton){
let digit = sender.currentTitle!
if userIsInTheMiddleOfTypingNumber {
display.text = display.text! + digit
}
else {
display.text = digit
userIsInTheMiddleOfTypingNumber = true
}
//println("digit = \(digit)")
}
@IBAction func operate(sender: UIButton) {
let operation = sender.currentTitle!
if userIsInTheMiddleOfTypingNumber {
enter()
}
switch operation {
case"×": performOperation() { $0 * $1 } - THIS IS THE CODE WHERE WE GOT ERROR
// case"÷":
// case"+":
// case"-":
default: break
}
func performOperation(operation: (Double, Double) -> Double) {
if operandStack.count >= 2 {
displayValue = operation(operandStack.removeLast(), operandStack.removeLast())
enter()
}
}
}
var operandStack = Array<Double>()
@IBAction func enter() {
userIsInTheMiddleOfTypingNumber = false
operandStack.append(displayValue)
println("operandStack = \(operandStack)")
}
var displayValue: Double {
get {
return NSNumberFormatter().numberFromString(display.text!)!.doubleValue
}
set{
display.text = "\(newValue)"
userIsInTheMiddleOfTypingNumber = false
}
}
}
导入UIKit
类ViewController:UIViewController{
@IBVAR显示:UILabel!
var USERISINTHEMIDLEOFTYPINGNUMBER:Bool=false
@iAction func appendDigit(发送方:UIButton){
让digit=sender.currentTitle!
如果用户是InTheMiddleOftypingNumber{
display.text=display.text!+数字
}
否则{
display.text=数字
USERISINTHEMIDLEOFTYPINGNUMBER=true
}
//println(“数字=\(数字)”)
}
@iAction func operate(发送方:UIButton){
让操作=sender.currentTitle!
如果用户是InTheMiddleOftypingNumber{
输入()
}
开关操作{
案例“×”:performOperation(){$0*$1}-这是我们得到错误的代码
//案例“÷”:
//格“+”:
//案例“-”:
默认值:中断
}
函数执行操作(操作:(双精度,双精度)->双精度){
如果操作数Stack.count>=2{
displayValue=操作(操作数堆栈.removeLast(),操作数堆栈.removeLast())
输入()
}
}
}
变量堆栈=数组()
@iAction func enter(){
USERISINTHEMIDLEOFTYPINGNUMBER=false
操作数堆栈.append(显示值)
println(“操作数堆栈=\(操作数堆栈)”)
}
var显示值:双精度{
得到{
返回NSNumberFormatter().numberFromString(display.text!)!.doubleValue
}
设置{
display.text=“\(newValue)”
USERISINTHEMIDLEOFTYPINGNUMBER=false
}
}
}
我认为您的performOperation功能应该在@iAction功能之外,如下所示:
@IBAction func operate(sender: UIButton) {
let operation = sender.currentTitle!
if userIsInTheMiddleOfTypingNumber {
enter()
}
switch operation {
case"×": performOperation() { $0 * $1 } - THIS IS THE CODE WHERE WE GOT ERROR
// case"÷":
// case"+":
// case"-":
default: break
}
}
func performOperation(operation: (Double, Double) -> Double)
{
if operandStack.count >= 2 {
displayValue = operation(operandStack.removeLast(), operandStack.removeLast())
enter()
}
}
我认为您的performOperation函数应该在@iAction函数之外,如下所示:
@IBAction func operate(sender: UIButton) {
let operation = sender.currentTitle!
if userIsInTheMiddleOfTypingNumber {
enter()
}
switch operation {
case"×": performOperation() { $0 * $1 } - THIS IS THE CODE WHERE WE GOT ERROR
// case"÷":
// case"+":
// case"-":
default: break
}
}
func performOperation(operation: (Double, Double) -> Double)
{
if operandStack.count >= 2 {
displayValue = operation(operandStack.removeLast(), operandStack.removeLast())
enter()
}
}
我认为您的performOperation函数应该在@iAction函数之外,如下所示:
@IBAction func operate(sender: UIButton) {
let operation = sender.currentTitle!
if userIsInTheMiddleOfTypingNumber {
enter()
}
switch operation {
case"×": performOperation() { $0 * $1 } - THIS IS THE CODE WHERE WE GOT ERROR
// case"÷":
// case"+":
// case"-":
default: break
}
}
func performOperation(operation: (Double, Double) -> Double)
{
if operandStack.count >= 2 {
displayValue = operation(operandStack.removeLast(), operandStack.removeLast())
enter()
}
}
我认为您的performOperation函数应该在@iAction函数之外,如下所示:
@IBAction func operate(sender: UIButton) {
let operation = sender.currentTitle!
if userIsInTheMiddleOfTypingNumber {
enter()
}
switch operation {
case"×": performOperation() { $0 * $1 } - THIS IS THE CODE WHERE WE GOT ERROR
// case"÷":
// case"+":
// case"-":
default: break
}
}
func performOperation(operation: (Double, Double) -> Double)
{
if operandStack.count >= 2 {
displayValue = operation(operandStack.removeLast(), operandStack.removeLast())
enter()
}
}