Swift字符串条件错误

时间:2016-11-27 04:54:30

标签: arrays swift debugging button conditional

在我的检查器项目中,我试图通过找到与按下的按钮的标题对应的i和j位置来找到所选的图块。

我的问题是代码在我设置的条件下继续失败。它只是遍历整个数组而没有找到匹配。

我的问题有两个问题。 A)我是否犯了一个基本错误,或者这种方法根本不可行? B)如果这是一些基本错误,修复是什么,如果不可行,我可以尝试另一种方法吗?

注意:我还没有更新到Xcode 3

Here is an example read-out of what happens, I clicked on the button in the upper left hand corner of the checkerboard.

我写的代码如下:

查看控制器页面(相关部分,并且在viewcontroller类中):

func placeButtons(){
    for i in 0..<(Int)(boardWidth){
    var column: [UIButton] = []
        for j in 0..<(Int)(boardHeight){ //make sure only place on 8x8 board
            let tileRect = CGRect(x: 40+(40*i), y: 60+(40*j), width: 40, height: 40)
            let button = UIButton(frame: tileRect) //place a button in the Rect
            button.setTitle("", forState: UIControlState.Normal) //Name button for cordinates on array
            button.titleLabel?.text = ("\(i), \(j)") //see above
            button.addTarget(self, action: #selector(self.buttonPressed(_:)), forControlEvents: UIControlEvents.TouchUpInside) //link button to action
            view.addSubview(button) //put button on screen
            column.append(button) //add button to array
        }
    field.append(column) //add column of buttons to array
    }
}

func buttonPressed(sender: UIButton!){
    selectedTile = "\(sender.titleLabel?.text)"
    print("got here-1")
    print("\(selectedTile)")
    selectFunction()
    /*if sender.titleLabel?.text != nil{
        saysTurn.text = "\(sender.titleLabel)"
        print("\(sender.titleLabel)")
    } else{

    } */ //works, get the actual play code up. Maybe click the first button for select checker to move, and the second select is where to move the checker to?
}

规则页面:

import Foundation

var piecePlacement = [[Int]](count: 8, repeatedValue: [Int](count: 8, repeatedValue: 0))
var selectedPiece: String = ""
var selectedMove: String = ""
var teamRequired: String = ""
var tester: String? = ""


func selectFunction(){ //something is failing in the selectedPiece    conditionals. Also, maybe switch from String to CGPoint to make it easier   to edit arrays? (and avoid having to constaly iterate over the entire array?)
    if  turnCounter % 2 == 0{
        teamRequired = "black"
    } else{
        teamRequired = "red"
    }
    print("got here-2")
    for i in 0..<(Int)(boardWidth){
        for j in 0..<(Int)(boardHeight){
            tester = "\(i), \(j)"
            print("\(tester)")
            if selectedTile == tester && ((piecePlacement[i][j] == 1 || piecePlacement[i][j] == 2)) && teamRequired == "black"{
                selectedPiece = selectedTile
                print ("\(selectedPiece)")
                print("\(i), \(j)")
                print("done-1")
            } else if selectedTile == tester && ((piecePlacement[i][j] == 3 || piecePlacement[i][j] == 4)) && teamRequired == "red"{
                selectedPiece = selectedTile
                print ("\(selectedPiece)")
                print("done-2")
            } else if selectedMove == "" && selectedTile == tester{
                selectedMove = selectedTile
                print ("\(selectedMove)")
                print("I Printed selectedMove \(i), \(j)")
            } else{
                print("got here-3")
            }
        }
    }
    if isLegalMove() == true{
        //func move
        print("move")
    }else{

    }
    print("end")
}

func isLegalMove() -> Bool{
    //if touched point is either x+1 && y+1 or x+1 && y-1 (if red, x-1 if black)  of the original checker's point, or if the checker is a king and the touched point is either x+/-1 && y+/-1. And the touched position is within the bounds of the board, the move is legal.
    return true 
}

0 个答案:

没有答案