UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Edutainment App - struct value

Forums > 100 Days of SwiftUI

I want to pass the questionRight value of int.random in Multiple lines strcut to questions struct and want to displat the actualAns in questions struct in multiple Lines struct. The values are bing passed but i dont get the actualAns right, it is like if questionLeft is 4 then 4 * 3 = 20 which is not right. What to do ? Multiple Lines Struct:

struct MultipleLines: View {
    @Binding var questionLef: Questions
    @Binding var userQ: String
    var questionRight: Int {
        Int.random(in: 0...12)
    }
    var passingValue: Questions {
        return Questions(questionLeft: questionLef.questionLeft, questionRight: questionRight)
    }
    var actualAns : Int {
        return (passingValue.questionLeft + 1) * passingValue.questionRight
    }
    var noQ: Int {
        Int(userQ) ?? 2
    }
    var body: some View {
        ForEach(0 ..< noQ, id: \.self) { _ in
            Text("\(questionLef.questionLeft + 1) * \(questionRight)")
            Text("\(passingValue.actualAns)")
        }
    }
    func changingNum(vaue: Int) -> Int {
        return Int.random(in: 0...12)
    }
    func calc(value1: Int, value2: Int) -> Int {
        let ans = value1 * value2
        return ans
    }
}

Questions struct:

struct Questions {
    var questionLeft: Int
    var questionRight: Int

    init(questionLeft: Int, questionRight: Int) {
        self.questionLeft = questionLeft
        self.questionRight = questionRight

    }
    var actualAns: Int {
        (questionLeft + 1) * questionRight
    }

}

2      

questionRight is a computed property. That means every time you call it, it will generate a new random number.

So, here:

Text("\(questionLef.questionLeft + 1) * \(questionRight)")

questionRight might be 8. But here:

var passingValue: Questions {
    return Questions(questionLeft: questionLef.questionLeft, questionRight: questionRight)
}

questionRight might be 3.

Which means your answer won't match your question because the numbers involved are different.

2      

i understand that but what can be done to match it ?

2      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

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

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

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.