为什么我在Swift中的条件语句不起作用?

时间:2015-02-14 14:26:05

标签: ios swift

var word1array = [colorwordw1, colorwordg1, colorwordb1]
var word1 = word1array [ofThree()]

[ofThree()]功能是:

func ofThree() -> Int {
    return Int(arc4random() % 3)
}

出于某种原因,以下条件语句始终计算为true

 @IBAction func White1Action(sender: AnyObject) {

    if (word1 == colorwordw1) {


        [moveTilesDown()]


        [RandomPlacementTwo()]


     }

我的代码出了什么问题?

由于

inits:

var colorwordw1 = "White"
var word1 = "White"

更多背景

var colorwordsarray:[String] = ["Blue","Red","Green","Yellow","Gray","Cyan","Magenta","Orange","Purple","White","Brown","Black"]

var colorwordw1 = colorwordsarray[random12]

random12 = ofTwelve()

func ofTwelve() -> Int {
    return Int(arc4random() % 12)
}

以下是所有代码:

class ViewController: UIViewController {



    @IBOutlet weak var whiteTile1: UIButton!

    @IBOutlet weak var greyTile1: UIButton!


    @IBOutlet weak var blackTile1: UIButton!

    @IBOutlet weak var whiteTile2: UIButton!

    @IBOutlet weak var greyTile2: UIButton!

    @IBOutlet weak var blackTile2: UIButton!

    @IBOutlet weak var whiteTile3: UIButton!

    @IBOutlet weak var greyTile3: UIButton!

    @IBOutlet weak var blackTile3: UIButton!

    @IBOutlet weak var whiteTile4: UIButton!

    @IBOutlet weak var greyTile4: UIButton!


    @IBOutlet weak var blackTile4: UIButton!


    @IBOutlet weak var whiteTile5: UIButton!

    @IBOutlet weak var greyTile5: UIButton!

    @IBOutlet weak var blackTile5: UIButton!
    @IBOutlet weak var colorText5: UILabel!
    @IBOutlet weak var colorText4: UILabel!
    @IBOutlet weak var colorText3: UILabel!
    @IBOutlet weak var colorText2: UILabel!
    @IBOutlet weak var colorText1: UILabel!



    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }



// function returns 1-3 randomly

    func ofThree() -> Int {
        return Int(arc4random() % 3)
    }

// variable array for color, with three options - used with above function to later define word variable

    var wordarray:[String] = ["White", "Grey", "Black"]

//     var ofThree = Int(arc4random() % 3)

    func ofTwelve() -> Int {
        return Int(arc4random() % 12)
    }

    var colorarray = [UIColor.blueColor(),UIColor.redColor(),UIColor.greenColor(),UIColor.yellowColor(),UIColor.grayColor(),UIColor.cyanColor(),UIColor.magentaColor(),UIColor.orangeColor(),UIColor.purpleColor(),UIColor.whiteColor(),UIColor.brownColor(),UIColor.blackColor()]

    var colorwordsarray:[String] = ["Blue","Red","Green","Yellow","Gray","Cyan","Magenta","Orange","Purple","White","Brown","Black"]

    // init word variable

    var word = "White"
    var word1 = "White"
    var word2 = "White"
    var word3 = "White"
    var word4 = "White"
    var word5 = "White"
    var colorw1 = UIColor.whiteColor()
    var colorw2 = UIColor.whiteColor()
    var colorw3 = UIColor.whiteColor()
    var colorw4 = UIColor.whiteColor()
    var colorw5 = UIColor.whiteColor()
    var colorg1 = UIColor.whiteColor()
    var colorg2 = UIColor.whiteColor()
    var colorg3 = UIColor.whiteColor()
    var colorg4 = UIColor.whiteColor()
    var colorg5 = UIColor.whiteColor()
    var colorb1 = UIColor.whiteColor()
    var colorb2 = UIColor.whiteColor()
    var colorb3 = UIColor.whiteColor()
    var colorb4 = UIColor.whiteColor()
    var colorb5 = UIColor.whiteColor()
    var colorwordw1 = "White"
    var colorwordw2 = "White"
    var colorwordw3 = "White"
    var colorwordw4 = "White"
    var colorwordw5 = "White"
    var colorwordg1 = "White"
    var colorwordg2 = "White"
    var colorwordg3 = "White"
    var colorwordg4 = "White"
    var colorwordg5 = "White"
    var colorwordb1 = "White"
    var colorwordb2 = "White"
    var colorwordb3 = "White"
    var colorwordb4 = "White"
    var colorwordb5 = "White"

    var random12 = 12


 func moveTilesDown(){

    colorText5.center.y += 142
    colorText4.center.y += 142
    colorText3.center.y += 142
    colorText2.center.y += 142
    colorText1.center.y += 142
    whiteTile1.center.y += 142
    whiteTile2.center.y += 142
    whiteTile3.center.y += 142
    whiteTile4.center.y += 142
    whiteTile5.center.y += 142
    greyTile1.center.y += 142
    greyTile2.center.y += 142
    greyTile3.center.y += 142
    greyTile4.center.y += 142
    greyTile5.center.y += 142
    blackTile1.center.y += 142
    blackTile2.center.y += 142
    blackTile3.center.y += 142
    blackTile4.center.y += 142
    blackTile5.center.y += 142
    [self.view .bringSubviewToFront(colorText1)]
    [self.view .bringSubviewToFront(colorText2)]
    [self.view .bringSubviewToFront(colorText3)]
    [self.view .bringSubviewToFront(colorText4)]
    [self.view .bringSubviewToFront(colorText5)]

    // get new random word and update label








     // if title row is too low, move row to the top

    if (whiteTile1.center.y >= 601) {
        whiteTile1.center.y = -71
        greyTile1.center.y = -71
        blackTile1.center.y = -71
        colorText1.center.y = -142
        random12 = ofTwelve()
        var colorw1 = colorarray[random12]
        var colorwordw1 = colorwordsarray[random12]
        random12 = ofTwelve()
        var colorg1 = colorarray[random12]
        var colorwordg1 = colorwordsarray[random12]
        random12 = ofTwelve()
        var colorb1 = colorarray[random12]
        var colorwordb1 = colorwordsarray[random12]
        var word1array = [colorwordw1, colorwordg1, colorwordb1]
        var word1 = word1array [ofThree()]
        whiteTile1.backgroundColor = colorw1
        greyTile1.backgroundColor = colorg1
        blackTile1.backgroundColor = colorb1
        [self.view .bringSubviewToFront(colorText1)]
        colorText1.text = word1
        colorText1.hidden = false



    }

//    if colorw1 == colorg1 {
//        
//        random12 = ofTwelve()
//        var colorw1 = colorarray[random12]
//        var colorwordw1 = colorwordsarray[random12]
//        var word1array = [colorwordw1, colorwordg1, colorwordb1]
//        word1 = word1array [ofThree()]
//        whiteTile1.backgroundColor = colorw1
//    }
//        
//    } else {
//        
//        if colorw1 == colorb1 {
//
//        random12 = ofTwelve()
//        var colorw1 = colorarray[random12]
//        var colorwordw1 = colorwordsarray[random12]
//        var word1array = [colorwordw1, colorwordg1, colorwordb1]
//        word1 = word1array [ofThree()]
//        whiteTile1.backgroundColor = colorw1
//        
//        }
//        
//    }
    if (whiteTile2.center.y >= 600) {
        whiteTile2.center.y = -71
        greyTile2.center.y = -71
        blackTile2.center.y = -71
        colorText2.center.y = -142
        random12 = ofTwelve()
        var colorw2 = colorarray[random12]
        var colorwordw2 = colorwordsarray[random12]
        random12 = ofTwelve()
        var colorg2 = colorarray[random12]
        var colorwordg2 = colorwordsarray[random12]
        random12 = ofTwelve()
        var colorb2 = colorarray[random12]
        var colorwordb2 = colorwordsarray[random12]
        var word2array = [colorwordw2, colorwordg2, colorwordb2]
        var word2 = word2array [ofThree()]
        whiteTile2.backgroundColor = colorw2
        greyTile2.backgroundColor = colorg2
        blackTile2.backgroundColor = colorb2
        [self.view .bringSubviewToFront(colorText2)]
        colorText2.text = word2
        colorText2.hidden = false


    }
    if (whiteTile3.center.y >= 600) {
        whiteTile3.center.y = -71
        greyTile3.center.y = -71
        blackTile3.center.y = -71
        colorText3.center.y = -142
        random12 = ofTwelve()
        var colorw3 = colorarray[random12]
        var colorwordw3 = colorwordsarray[random12]
        random12 = ofTwelve()
        var colorg3 = colorarray[random12]
        var colorwordg3 = colorwordsarray[random12]
        random12 = ofTwelve()
        var colorb3 = colorarray[random12]
        var colorwordb3 = colorwordsarray[random12]
        var word3array = [colorwordw3, colorwordg3, colorwordb3]
        var word3 = word3array [ofThree()]
        whiteTile3.backgroundColor = colorw3
        greyTile3.backgroundColor = colorg3
        blackTile3.backgroundColor = colorb3
        [self.view .bringSubviewToFront(colorText3)]
        colorText3.text = word3
        colorText3.hidden = false


    }
    if (whiteTile4.center.y >= 600) {
        whiteTile4.center.y = -71
        greyTile4.center.y = -71
        blackTile4.center.y = -71
        colorText4.center.y = -142
        random12 = ofTwelve()
        var colorw4 = colorarray[random12]
        var colorwordw4 = colorwordsarray[random12]
        random12 = ofTwelve()
        var colorg4 = colorarray[random12]
        var colorwordg4 = colorwordsarray[random12]
        random12 = ofTwelve()
        var colorb4 = colorarray[random12]
        var colorwordb4 = colorwordsarray[random12]
        var word4array = [colorwordw4, colorwordg4, colorwordb4]
        var word4 = word4array [ofThree()]
        whiteTile4.backgroundColor = colorw4
        greyTile4.backgroundColor = colorg4
        blackTile4.backgroundColor = colorb4
        [self.view .bringSubviewToFront(colorText4)]
        colorText4.text = word4
        colorText4.hidden = false


    }
    if (whiteTile5.center.y >= 600) {
        whiteTile5.center.y = -71
        greyTile5.center.y = -71
        blackTile5.center.y = -71
        colorText5.center.y = -142
        random12 = ofTwelve()
        var colorw5 = colorarray[random12]
        var colorwordw5 = colorwordsarray[random12]
        random12 = ofTwelve()
        var colorg5 = colorarray[random12]
        var colorwordg5 = colorwordsarray[random12]
        random12 = ofTwelve()
        var colorb5 = colorarray[random12]
        var colorwordb5 = colorwordsarray[random12]
        var word5array = [colorwordw5, colorwordg5, colorwordb5]
        var word5 = word5array [ofThree()]
        whiteTile5.backgroundColor = colorw5
        greyTile5.backgroundColor = colorg5
        blackTile5.backgroundColor = colorb5
        [self.view .bringSubviewToFront(colorText5)]
        colorText5.text = word5
        colorText5.hidden = false
    }

    }








// Below are the button actions.  First they check if the correct color was selected.  If false, no action.  If true, it moves all tiles down and selects a new color randomly and updates the label accordingly.  After that, it calls the RandomPlacement function that scrambles the order of the next row of tiles displayed.




    @IBAction func White4Action(sender: AnyObject) {
//        if (whiteTile4.center.y >= 490 && word == "White") {

        if (word4 == colorwordw4){


            [moveTilesDown()]


            [RandomPlacementFive()]


        }





    }
    @IBAction func Grey4Action(sender: AnyObject) {

        if (word4 == colorwordg4) {


            [moveTilesDown()]


            [RandomPlacementFive()]

        }



    }

    @IBAction func Black4Action(sender: AnyObject) {

        if (word4 == colorwordb4) {


            [moveTilesDown()]


            [RandomPlacementFive()]
        }



    }


    @IBAction func White3Action(sender: AnyObject) {

        if (word3 == colorwordw3){


            [moveTilesDown()]


            [RandomPlacementFour()]



        }



    }
    @IBAction func Grey3Action(sender: AnyObject) {

        if (word3 == colorwordg3) {


            [moveTilesDown()]


            [RandomPlacementFour()]



        }



    }
    @IBAction func Black3Action(sender: AnyObject) {

        if (word3 == colorwordb3) {


            [moveTilesDown()]


            [RandomPlacementFour()]

        }



    }

    @IBAction func White2Action(sender: AnyObject) {

        if (word2 == colorwordw2) {

            [moveTilesDown()]


            [RandomPlacementThree()]


        }



    }

    @IBAction func Grey2Action(sender: AnyObject) {

        if (word2 == colorwordg2) {


            [moveTilesDown()]


            [RandomPlacementThree()]


        }



    }

    @IBAction func Black2Action(sender: AnyObject) {

        if (word2 == colorwordb2) {


            [moveTilesDown()]


            [RandomPlacementThree()]


        }



    }

    @IBAction func White1Action(sender: AnyObject) {

        if (word1 == colorwordw1) {


            [moveTilesDown()]


            [RandomPlacementTwo()]


         }

    }
    @IBAction func Grey1Action(sender: AnyObject) {

        if (word1 == colorwordg1) {

            [moveTilesDown()]


            [RandomPlacementTwo()]


      }

    }

    @IBAction func Black1Action(sender: AnyObject) {

        if (word1 == colorwordb1) {

            [moveTilesDown()]


            [RandomPlacementTwo()]


             }

    }

    @IBAction func White5Action(sender: AnyObject) {


        if (word5 == colorwordw5) {


            [moveTilesDown()]


            [RandomPlacementOne()]


        }
    }


    @IBAction func Grey5Action(sender: AnyObject) {

        if (word5 == colorwordg5) {



            [moveTilesDown()]


            [RandomPlacementOne()]




    }
    }


    @IBAction func Black5Action(sender: AnyObject) {

        if (word5 == colorwordb5) {




        [moveTilesDown()]

            [RandomPlacementOne()]

        }



}



    func RandomPlacementOne(){


        // question = wordarray[ofThree()]


        var RandomNumber = arc4random() % 6
        switch(RandomNumber){
        case 0:
            blackTile1.center.x = 161
            whiteTile1.center.x = 268
            greyTile1.center.x = 54


        case 1:
            blackTile1.center.x = 268
            whiteTile1.center.x = 161
            greyTile1.center.x = 54


        case 2:
            blackTile1.center.x = 54
            whiteTile1.center.x = 268
            greyTile1.center.x = 161


        case 3:
            blackTile1.center.x = 54
            whiteTile1.center.x = 161
            greyTile1.center.x = 268


        case 4:
            blackTile1.center.x = 268
            whiteTile1.center.x = 54
            greyTile1.center.x = 161


        case 5:
            blackTile1.center.x = 161
            whiteTile1.center.x = 54
            greyTile1.center.x = 268


        default: break



    }
//         [colorWord()]

}


    // Below are the random placement functions.  It first chooses a random number out of 6 and depending on the number, reorders the colors on the row.

    func RandomPlacementTwo(){


        // question = wordarray[ofThree()]


        var RandomNumber = arc4random() % 6
        switch(RandomNumber){
        case 0:
            blackTile2.center.x = 161
            whiteTile2.center.x = 268
            greyTile2.center.x = 54


        case 1:
            blackTile2.center.x = 268
            whiteTile2.center.x = 161
            greyTile2.center.x = 54


        case 2:
            blackTile2.center.x = 54
            whiteTile2.center.x = 268
            greyTile2.center.x = 161


        case 3:
            blackTile2.center.x = 54
            whiteTile2.center.x = 161
            greyTile2.center.x = 268


        case 4:
            blackTile2.center.x = 268
            whiteTile2.center.x = 54
            greyTile2.center.x = 161


        case 5:
            blackTile2.center.x = 161
            whiteTile2.center.x = 54
            greyTile2.center.x = 268


        default: break



        }
//         [colorWord()]

}
    func RandomPlacementThree(){


        // question = wordarray[ofThree()]


        var RandomNumber = arc4random() % 6
        switch(RandomNumber){
        case 0:
            blackTile3.center.x = 161
            whiteTile3.center.x = 268
            greyTile3.center.x = 54


        case 1:
            blackTile3.center.x = 268
            whiteTile3.center.x = 161
            greyTile3.center.x = 54


        case 2:
            blackTile3.center.x = 54
            whiteTile3.center.x = 268
            greyTile3.center.x = 161


        case 3:
            blackTile3.center.x = 54
            whiteTile3.center.x = 161
            greyTile3.center.x = 268


        case 4:
            blackTile3.center.x = 268
            whiteTile3.center.x = 54
            greyTile3.center.x = 161


        case 5:
            blackTile3.center.x = 161
            whiteTile3.center.x = 54
            greyTile3.center.x = 268


        default: break



        }

//         [colorWord()]

}
    func RandomPlacementFour(){



        // question = wordarray[ofThree()]


        var RandomNumber = arc4random() % 6
        switch(RandomNumber){
        case 0:
            blackTile4.center.x = 161
            whiteTile4.center.x = 268
            greyTile4.center.x = 54


        case 1:
            blackTile4.center.x = 268
            whiteTile4.center.x = 161
            greyTile4.center.x = 54


        case 2:
            blackTile4.center.x = 54
            whiteTile4.center.x = 268
            greyTile4.center.x = 161


        case 3:
            blackTile4.center.x = 54
            whiteTile4.center.x = 161
            greyTile4.center.x = 268


        case 4:
            blackTile4.center.x = 268
            whiteTile4.center.x = 54
            greyTile4.center.x = 161


        case 5:
            blackTile4.center.x = 161
            whiteTile4.center.x = 54
            greyTile4.center.x = 268


        default: break



        }

// [colorWord()]
}
    func RandomPlacementFive(){

        // question = wordarray[ofThree()]


        var RandomNumber = arc4random() % 6
        switch(RandomNumber){
        case 0:
            blackTile5.center.x = 161
            whiteTile5.center.x = 268
            greyTile5.center.x = 54


        case 1:
            blackTile5.center.x = 268
            whiteTile5.center.x = 161
            greyTile5.center.x = 54


        case 2:
            blackTile5.center.x = 54
            whiteTile5.center.x = 268
            greyTile5.center.x = 161


        case 3:
            blackTile5.center.x = 54
            whiteTile5.center.x = 161
            greyTile5.center.x = 268


        case 4:
            blackTile5.center.x = 268
            whiteTile5.center.x = 54
            greyTile5.center.x = 161


        case 5:
            blackTile5.center.x = 161
            whiteTile5.center.x = 54
            greyTile5.center.x = 268


        default: break



        }
//        [colorWord()]


}

}

1 个答案:

答案 0 :(得分:2)

无论你在声明它时赋予变量什么值,它都是按下按钮时它仍然具有的值。在按下按钮之前,没有任何内容可以更改任何按钮的值。您的类中没有方法作为视图控制器生命周期事件的一部分被调用(并且没有构造函数)。

因此,当按下第一个按钮时,分配了值word1"White"的{​​{1}}(也被赋予值colorwordw1将是相同的值。这意味着"White"应为其返回==

点击按钮后,所有按钮都会调用true功能。我假设您可能希望此函数更改按钮按下时比较的实例变量的值?但它没有改变任何实例变量。

查看我粘贴到Xcode中的代码截图中的语法高亮显示:

enter image description here

绿色变量是实例变量。白色变量是仅作用于函数的变量(在这种特定情况下,它们仅限于moveTilesDown块中的范围)。

我们在此if块中声明了8个新变量,这些变量恰好与我们的某些实例变量同名。

如果我们回到处理按钮触摸的方法并设置断点(或只是打印值),我们会发现变量都是if。 Nothign永远改变变量。

然而,您的"White"方法 负责更改按钮上的颜色和文字。但是你的按键触摸方法不能将按钮上的文本与局部变量或按钮的颜色进行比较,而是与永不改变的变量进行比较。

因此,如果我们打算在我们的moveTilesDown方法中更改我们的实例变量,我们需要将这些以moveTilesDown开头的8行更改为引用实例变量而不是创建新的局部变量。

例如:

var