NEW: My new book Pro SwiftUI is out now – level up your SwiftUI skills today! >>

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

SPONSORED From March 20th to 26th, you can 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!

Click to save your free spot now

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.