The error is clear. But you'll need a few more sessions to get used to trouble shooting. Let's help!
If you hold your option key down, and click on j, XCode will tell you that j is of type Int. Yeah, it's an integer.
for j in 1...100 {
// If you hold your option key down and click on j,
// XCode will tell you that j is of type Int
switch j {
case j.isMultiple(of: 3) && j.isMultiple(of: 5):
print ("FizzBuzz")
// more code
} // end switch
} // end for
Now add this line just before the switch j
statement: let whatsThis = j.isMultiple(of: 3)
Now hold the option key down and click on the variable whatsThis
.
What does XCode reveal? What type is whatsThis
?
Now you see why the compiler is not happy. You are providing an integer to the switch statement.
But within the switch statement you are trying to compare j to a boolean value. You cannot compare integers to booleans!
Case solved.
PS: Whilst learning Swift use what ever variable names you wish. However, and hear me out on this, I think you lose the big picture when you use single letter names for variables.
For example, had you named the variable targetInteger
your code would then read:
for targetInteger in 1...100 {
switch targetInteger {
case targetInteger.isMultiple(of: 3) && targetInteger.isMultiple(of: 5):
print ("FizzBuzz")
// more code
} // end switch
} // end for
One might argue that this is more readable. Plus it might have clued you in that you are comparing an Integer (targetInteger) to a boolean (targetInteger.isMultiple(of:) returns either true
or false
. Hey! that's not an integer! Just food for thought.