TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

Trying to apply what I've learned elsewhere with no luck

Forums > 100 Days of SwiftUI

So, we have done so many functions that I thought I could pull this off. I am helping my students create an app that will track their goals/scores on a test. They would put in their goal and then their score when the test was complete. I want that double to give me my trim value. I really thought I could pull what we did from Cupcake Corner to make this happen, but I'm floundering.

struct MMathView: View {

@AppStorage("mapBOY") var mapBOY: Int = 0
@AppStorage("mapBOYg") var mapBOYg: Int = 0

var setTrim: Double {
    var setTrim = Double(mapBOY/mapBOYg)
    return setTrim
}

  func setTrim() -> Double {
        return Double(mapBOY/mapBOYg)
        let result = setTrim()
    }

///// later I use it here

      Circle()// Beginning band-goal
                        .trim(from: 0, to: setTrim)
                        .stroke(lineWidth: 40)
                        .frame(width: 450)
                        .rotationEffect(.init(degrees: -90))

So it should fill it to 0.8 but it doesn't change the value at all. If I switch the values, I do get a full fill so the trim seems to be getting a value... just not the right way?

3      

Integer division throws away any remainder, so, depending on the values you feed it, your mapBOY/mapBOYg is often going to end up as 0. Which when you convert to a Double, becomes 0.0. As in the example where you are expecting 0.8; you will never get that as an answer when converting the result of integer division to a Double.

4      

As @roosterboy says is true change the computed property to this

var setTrim: Double {
    Double(mapBOY) / Double(mapBOYg)
}

So you can do this

struct ContentView: View {
    @AppStorage("mapBOY") var mapBOY: Int = 8
    @AppStorage("mapBOYg") var mapBOYg: Int = 10

    var setTrim: Double {
        Double(mapBOY) / Double(mapBOYg)
    }

    var body: some View {
        ZStack {
            Circle()
                .stroke(Color.blue.opacity(0.2), style: StrokeStyle(lineWidth: 40, lineCap: .round))
                .frame(width: 300)
                .rotationEffect(.init(degrees: -90))

            Circle()
                .trim(from: 0.0, to: setTrim)
                .stroke(Color.blue.gradient, style: StrokeStyle(lineWidth: 40, lineCap: .round))
                .frame(width: 300)
                .rotationEffect(.init(degrees: -90))

        }
    }
}

4      

Thank yall! I appreciate it. Order of operations got me!

3      

Hacking with Swift is sponsored by RevenueCat.

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

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.