1

I am trying to make a Sets! game in which I need to choose 3 cards from the UI and compare between them (I am comparing the names of the images) for example:

UIButton 1 w/ image name : "1123"

UIButton 2 w/ image name : "1223"

UIButton 3 w/ image name : "1133"

Using this func:

func checkingIfMatch(cardOne : String, cardTwo : String, cardThree : String) -> Bool {
        let first = cardOne.compactMap{Int(String($0))} //making an array from the UIImage file name
        let second = cardTwo.compactMap{Int(String($0))}
        let third = cardThree.compactMap{Int(String($0))}
    if (first[0] == second[0] && second[0] == third[0]) || (first[0] != second[0] && second[0] != third[0]) {
        if (first[1] == second[1] && second[1] == third[1]) || (first[1] != second[1] && second[1] != third[1]) {
            if (first[2] == second[2] && second[2] == third[2]) || (first[2] != second[2] && second[2] != third[2]) {
                if (first[3] == second[3] && second[3] == third[3]) || (first[3] != second[3] && second[3] != third[3]) {
                    print("match!")
                    return true
                }
            }
        }
    }
    print("no match!")
    return false
}

}

I am trying to get the name of the UIButton's image when tapping on the button in order to do it but seems like I cant find a way to do it. Any help will be appreciated, if there is no way to do it would like to hear different ideas to do it.

3
  • can you explain what are you trying to do i am finding problems understanding that 'IF' condition. Commented Feb 24, 2019 at 7:45
  • You may have loaded these assets from files (or the assets catalog), but once they’re loaded, the images, themselves, don’t have names. You’ll have to keep track of the names yourself. Commented Feb 24, 2019 at 7:51
  • I need to check if index[0] of all 3 arrays equal or none of them equal for example 1, 2 , 3 is true, 3 , 3 , 3 is true as well but 1 , 1 , 2 is false. Commented Feb 24, 2019 at 7:53

1 Answer 1

1

I don't know what are you trying to achieve, but you can use tag get what button was tapped.

In the Storyboard, connect your three buttons to the same @IBAction with sender type of UIButton, then simply case around the tag to know what button user tapped.

@IBAction func tappedButton(_ sender:  UIButton) {
    switch sender.tag {
        case 0: // image One
        case 1: // image Two
        case 2: // image Three
        default: // default condition where there is no matching cases
    }
}

Just drag the buttons to the function, Don't forget to give them tags.

btn.tag = 0

Or directly from the IB.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.