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

SOLVED: Computed var referencing “parent” var

Forums > Swift

To keep my views clean, I would like to calculate a score using a computed var defined in a "RaceScore" class. However the value depends not only properties in that raceScore but also on values in the Race object it is part of. For example the number of raceScores in that Race. Am i trying to do the impossible, or is there a way? I thought of parent.raceScores.count but thats about inheritance... Thanks

2      

Some of the code if specifics help...

class RaceScore: Codable, ObservableObject, Hashable, Identifiable {
    var id = UUID()
    var skipID: UUID
    var sailNum: Int = 0
    var raceNum: Int = 0
    var heat: Heat = .X
    var raceFinishers: Int = 0  // set from ??

    var raceStaticScore: Double = 0.0
    var raceFinish: Int = 0
    var raceLetterScore: RaceLetterScore = .NotScored
    var raceRawScore: Double {
            switch raceLetterScore {
            case .👍:
                return Double(raceFinish)
            case .NotScored:
                return 0.0
            case .RET, .DNC, .DNF, .DNS, .OCS, .DNE, .DSQ:
                return Double(parent.raceScores.count) + regattaList.selectedRegatta.letterScoreRules[newlScore]!
            case .RDG:
                return raceStaticScore
            }
    var canThrowOut: Bool {
        if raceLetterScore == .DNE {
            return false
        } else {
            return true
        }
    }
    var isThrownOut: Bool = false
class Race: Codable, Identifiable, Hashable {
    var id = UUID()
    var raceNum: Int = 0
    var heat: Heat = .X
    var isSeed: Bool = false
    var nextPlace: Int = 1
    var raceScores: [RaceScore] = []
    var unscoredSkippers: [RaceScore] {
        let unSkip = raceScores.filter {$0.raceLetterScore == .NotScored}
        return unSkip.sorted(by: {$0.sailNum < $1.sailNum} )
    }
    var notScored: Int {
        unscoredSkippers.count
    }
    var scoredSkippers: [RaceScore] {
        return raceScores.filter {$0.raceLetterScore != .NotScored}.sorted {$0.raceFinish < $1.raceFinish}
    }
    var areScored: Int {
            scoredSkippers.count
    }

    static func ==(lhs: Race, rhs: Race) -> Bool {
        return lhs.raceNum == rhs.raceNum
    }
    func hash(into hasher: inout Hasher) {
        hasher.combine(raceNum)
    }

}

2      

However the value depends not only properties in that raceScore but also on values in the Race object it is part of.

You should define your computed property on the Race object.

Or it should be a method on RaceScore that takes a Race parameter.

3      

Ah yes. Thanks @roosterboy for helping see the error of my ways...

2      

Hacking with Swift is sponsored by Superwall

SPONSORED Superwall lets you build & test paywalls without shipping updates. Run experiments, offer sales, segment users, update locked features and more at the click of button. Best part? It's FREE for up to 250 conversions / mo and the Superwall team builds out 100% custom paywalls – free of charge.

Learn More

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.