NEW: Learn to build amazing SwiftUI apps for macOS with my new book! >>

SOLVED: Checkpoint 3, how to solve with switch case?

Forums > 100 Days of SwiftUI

Finish until checkpoint 3.

I just use if, else if, else and all work fine.

Now I want to try to use switch case solution... anyone have idea how to do this?

Thank you

1      

This code below did not work.

for j in 1...100 {
    switch j {
    case j.isMultiple(of: 3) && j.isMultiple(of: 5):
        print ("FizzBuzz")

    case j.isMultiple(of: 3):
        print ("Fizz")

    case j.isMultiple(of: 5):
        print ("Buzz")

    default:
        print (j)
    }
}

error: expression pattern of type "BOOL" cannot match values of type "INT"

1      

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.

1      

I did checkpoint 3 two ways:

for number in 1...100 {
    switch 0 {
    case number % 3 + number % 5:
        print("FizzBuzz")
    case number % 3:
        print("Fizz")
    case number % 5:
        print("Buzz")
    default:
        print(number)
    }
}
for number in 1...100 {
    if number.isMultiple(of: 3) && number.isMultiple(of: 5) {
        print("FizzBuzz")
    } else if number.isMultiple(of: 3) {
        print("Fizz")
    } else if number.isMultiple(of: 5) {
        print("Buzz")
    } else {
        print(number)
    }
}

1      

Thank you, this give me idea to do my switch case in checkpoint 3.

This is how I did it.

for number in 1...100 {
    switch true {
    case number.isMultiple(of: 3) && number.isMultiple(of: 5):
        print("FizzBuzz")
    case number.isMultiple(of: 3):
        print("Fizz")
    case number.isMultiple(of: 5):
        print("Buzz")
    default:
        print(number)
    }
}

1      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Spend less time managing in-app purchase infrastructure so you can focus on building your app. RevenueCat gives everything you need to easily implement, manage, and analyze in-app purchases and subscriptions without managing servers or writing backend code.

Get Started

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.