BLACK FRIDAY SALE: Save 50% on all my Swift books and bundles! >>

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...


Save 50% in my Black Friday sale.

SAVE 50% To celebrate Black Friday, all our books and bundles are half price, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.

Save 50% on all our books and bundles!

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.