在我的代码结束时,我试图在我们获得3次罢工时将其提升为1。如果声明我在底部收到错误。它说预期的声明
//
// ViewController.swift
// helloWordDemo
//
// Created by Developer on 6/8/14.
// Copyright (c) 2014 AECApps. All rights reserved.
//
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBOutlet var labelDispaly : UILabel = nil
// dispaly Strikes
var counter = 1
@IBAction func buttonPressed(sender : AnyObject) {
labelDispaly.text = "Strikes \(counter++)"
}
//button to add strikes
@IBOutlet var OutsDispaly : UILabel = nil
var outsCounter = 1
//outs dispaly
@IBAction func outsButtonPressed(sender : AnyObject) {
OutsDispaly.text = "Outs \(outsCounter++)"
}
//button to add outs
if counter = 3 {
outsCounter ++
}
}
答案 0 :(得分:1)
问题是if语句不在函数内部。当语句在class
之外时,这是可以的,但在这种情况下不是这样。编写一个函数来运行语句。将此添加到您的班级:
func updateOuts(){
if counter == 3 {
outsCounter++
}
}
此外,在if语句中,您应该使用==
而不是=
。这是因为==
比较了两个值,=
设置了变量。