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 Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

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.