Jamie
Jamie

Reputation: 10906

Swift return bool in method

I've made this method:

func checkScore(player: Int) -> Bool {

        var checkedFields: [Int] = []
        var won: Bool = false

        for var i = 0; i <= 9; i += 1 {

            if(winningCombinations[i] == player) {
                checkedFields.append(i)
            }

        }

        for value in winningCombinations {

            var hits = 0

            for n in checkedFields {

                if value.contains(n){
                    hits += 1
                }

            }

            if hits == 3 {
                won = true
            }

        }

        return won
    }

But when I try to build it everything becomes white and the build crashes. Am I doing something wrong here? I pass the value like this:

 if self.checkScore(player) {
                print("Won!")
    }

(I see no error message!)

Upvotes: 0

Views: 85

Answers (1)

Akshansh Thakur
Akshansh Thakur

Reputation: 5341

Your func checkScore(player: Int) accepts player, which is of type Int.

In your code you also say : if(winningCombinations[i] == player), meaning that you expect the elements in array winningCombinations to also be of type Int

But then you say

for value in winningCombinations {
var hits = 0

            for n in checkedFields {

                if value.contains(n){

If value is an element in winningCombination, it means that value is an int.. how can you say value.contains(n). Int cannot perform contains operation. Arrays can.

Upvotes: 1

Related Questions