虽然Loop in Swift需要声明?

时间:2014-11-12 03:23:31

标签: ios xcode swift

我有一个井字游戏,有9个按钮和9个图像视图,每个按钮对应一个图像视图。按下按钮时,它将图像视图更改为X或O的图片,对应于哪个玩家按下按钮。现在,我为每个按钮设置了一个变量,根据按下按钮的人,它会将变量更改为1或10(从0开始)。我需要一个for或while语句来不断检查是否已经发生了胜利。这是我的while语句的代码,但它不起作用。任何建议都有帮助!

var i = 0
while (i < 5)
{
//execute code here
i++
}

此代码显示的所有内容都是红色错误,显示:预期声明,重点是W in while。在尝试运行时,代码无法构建。

import UIKit




class ViewController: UIViewController {
var i = 0
while (i < 5)
{
//execute code here
i++
}




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.
}
//end overrides




@IBOutlet var topLeft: UIImageView!
@IBOutlet var topLeftB: UIButton!

@IBOutlet var topCenter: UIImageView!
@IBOutlet var topCenterB: UIButton!

@IBOutlet var topRight: UIImageView!
@IBOutlet var topRightB: UIButton!

@IBOutlet var middleLeft: UIImageView!
@IBOutlet var middleLeftB: UIButton!

@IBOutlet var middleCenter: UIImageView!
@IBOutlet var middleCenterB: UIButton!

@IBOutlet var middleRight: UIImageView!
@IBOutlet var middleRightB: UIButton!

@IBOutlet var bottomLeft: UIImageView!
@IBOutlet var bottomLeftB: UIButton!

@IBOutlet var bottomCenter: UIImageView!
@IBOutlet var bottomCenterB: UIButton!

@IBOutlet var bottomRight: UIImageView!
@IBOutlet var bottomRightB: UIButton!


@IBOutlet var resetButton: UIButton!

var playerTurn = 0
var topLeftScore = 0
var topCenterScore = 0
var topRightScore = 0
var middleLeftScore = 0
var middleCenterScore = 0
var middleRightScore = 0
var bottomLeftScore = 0
var bottomCenterScore = 0
var bottomRightScore = 0


/*    @IBAction func checkScore(sender: UIButton) {
    if topLeftScore + topCenterScore + topRightScore == 3{
        topLeft.image = UIImage(named: "")
    }




}
*/

@IBAction func resetButtonAct(sender: UIButton) {
playerTurn = 0
topLeftB.enabled = true
topCenterB.enabled = true
topRightB.enabled = true
middleLeftB.enabled = true
middleCenterB.enabled = true
middleRightB.enabled = true
bottomLeftB.enabled = true
bottomCenterB.enabled = true
bottomRightB.enabled = true
    topLeft.image = UIImage(named: "")
    topCenter.image = UIImage(named: "")
    topRight.image = UIImage(named: "")
    middleRight.image = UIImage(named: "")
    middleLeft.image = UIImage(named: "")
    middleCenter.image = UIImage(named: "")
    bottomLeft.image = UIImage(named: "")
    bottomCenter.image = UIImage(named: "")
    bottomRight.image = UIImage(named: "")

    topLeftB.hidden = false
    topCenterB.hidden = false
    topRightB.hidden = false
    middleLeftB.hidden = false
    middleCenterB.hidden = false
    middleRightB.hidden = false
    bottomLeftB.hidden = false
    bottomCenterB.hidden = false
    bottomRightB.hidden = false
}


@IBAction func topLeft(sender: UIButton) {

    if playerTurn == 0 {

    topLeft.image = UIImage(named: "Xpix.png")
    playerTurn = playerTurn + 1
    topLeftB.enabled = false
    topLeftB.hidden = true
         topLeftScore = 1

    }
    else if playerTurn == 1 {
        topLeft.image = UIImage(named: "Opix.png")
         playerTurn = playerTurn - 1
        topLeftB.enabled = false
        topLeftB.hidden = true
        topLeftScore = 10
    }
 }

 @IBAction func topCenter(sender: UIButton) {
    topCenterB.hidden = true //included in both
    if playerTurn == 0 {

        topCenter.image = UIImage(named: "Xpix.png")
        playerTurn = playerTurn + 1
        topCenterB.enabled = false

         topCenterScore = 1

    }
    else if playerTurn == 1{
        topCenter.image = UIImage(named: "Opix.png")
        playerTurn = playerTurn - 1
        topCenterB.enabled = false
          topCenterScore = 10
    }

 }

 @IBAction func topRight(sender: UIButton) {
    topRightB.hidden = true //included in both
    if playerTurn == 0 {

        topRight.image = UIImage(named: "Xpix.png")
        playerTurn = playerTurn + 1
        topRightB.enabled = false
        topRightScore = 1


    }
    else if playerTurn == 1 {
        topRight.image = UIImage(named: "Opix.png")
        playerTurn = playerTurn - 1
        topRightB.enabled = false
        topRightScore = 10
    }



}

@IBAction func middleLeft(sender: UIButton) {
 middleLeftB.hidden = true   //included in both
    if playerTurn == 0 {

        middleLeft.image = UIImage(named: "Xpix.png")
        playerTurn = playerTurn + 1
        middleLeftB.enabled = false
        middleLeftScore = 1

    }
    else if playerTurn == 1 {
        middleLeft.image = UIImage(named: "Opix.png")
        playerTurn = playerTurn - 1
        middleLeftB.enabled = false
        middleLeftScore = 10
    }
}

@IBAction func middleCenter(sender: UIButton) {
   middleCenterB.hidden = true //included in both
    if playerTurn == 0 {

        middleCenter.image = UIImage(named: "Xpix.png")
        playerTurn = playerTurn + 1
        middleCenterB.enabled = false
        middleCenterScore = 1
    }
    else if playerTurn == 1 {
        middleCenter.image = UIImage(named: "Opix.png")
        playerTurn = playerTurn - 1
        middleCenterScore = 10
    }

 }


@IBAction func middleRight(sender: UIButton) {
middleRightB.hidden = true //included in both
if playerTurn == 0 {

        middleRight.image = UIImage(named: "Xpix.png")
        playerTurn = playerTurn + 1
        middleRightB.enabled = false
         middleRightB.hidden = true
        middleRightScore = 1
    }
    else if playerTurn == 1 {
        middleRight.image = UIImage(named: "Opix.png")
        playerTurn = playerTurn - 1
        middleRightB.enabled = false
         middleRightB.hidden = true
        middleRightScore = 10
    }


}


@IBAction func bottomLeft(sender: UIButton) {
   bottomLeftB.hidden = true
    if playerTurn == 0 {

        bottomLeft.image = UIImage(named: "Xpix.png")
        playerTurn = playerTurn + 1
        bottomLeftB.enabled = false
        bottomRightScore = 1

    }
    else if playerTurn == 1 {
        bottomLeft.image = UIImage(named: "Opix.png")
        playerTurn = playerTurn - 1
    bottomLeftB.enabled = false
        bottomRightScore = 10
    }

}


@IBAction func bottomCenter(sender: UIButton) {
bottomCenterB.hidden = true
    if playerTurn == 0 {

        bottomCenter.image = UIImage(named: "Xpix.png")
        playerTurn = playerTurn + 1
        bottomCenterB.enabled = false
        bottomCenterScore = 1

    }
    else if playerTurn == 1 {
        bottomCenter.image = UIImage(named: "Opix.png")
        playerTurn = playerTurn - 1
    bottomCenterB.enabled = false
        bottomCenterScore = 10
    }


}


@IBAction func bottomRight(sender: UIButton) {
    bottomRightB.hidden = true
    if playerTurn == 0 {

        bottomRight.image = UIImage(named: "Xpix.png")
        playerTurn = playerTurn + 1
        bottomRightB.enabled = false
        bottomRightScore = 1

    }
    else if playerTurn == 1 {
        bottomRight.image = UIImage(named: "Opix.png")
        playerTurn = playerTurn - 1
    bottomRightB.enabled = false
        bottomRightScore = 10
    }

    }//ends last function




}

1 个答案:

答案 0 :(得分:2)

您已在类级别编写该代码(while循环)。您需要将其移动到函数以便编译代码。

class ViewController: UIViewController {
// your code was here

    function functionLevel() -> Void {
         var i = 0
         while (i < 5) {
              i++
         }

    }
}