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

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


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 {
    var scoredSkippers: [RaceScore] {
        return raceScores.filter {$0.raceLetterScore != .NotScored}.sorted {$0.raceFinish < $1.raceFinish}
    var areScored: Int {

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



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.


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


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!

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.