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

trying to get a team average.

Forums > SwiftUI

how can i access the teams in the teammateArray? then access the latest score.totalScore record to return an average?

Group {
                    ForEach(myRecords, id: \.self) { team in
                        Section(header: HStack {
                            Text(team.wrappedTeamName)
                            Spacer()
                            Text("My average: \(team.wrappedTeamName.count / Int(team.teammateArray[0].score.totalScore))")
                                .font(.subheadline)
                                .foregroundColor(.secondary)
                        }) {
                            ForEach(team.teammateArray, id: \.userId) { teammate in
                                NavigationLink(destination: TeammateView(teammate: teammate, genders: teammate.gender)) {
                                    Text("\(UserDefaults.standard.string(forKey: "lastName") ?? ""), \(UserDefaults.standard.string(forKey: "firstName") ?? "")")
                                        .font(.subheadline)
                                }
                            }
                        }
                    }
                }

                // this is how Paul reccomended to sort the NSSet

    @nonobjc public class func fetchRequest() -> NSFetchRequest<Team> {
        return NSFetchRequest<Team>(entityName: "Team")
    }

    @NSManaged public var createdAt: Date?
    @NSManaged public var id: UUID?
    @NSManaged public var teamName: String?
    @NSManaged public var teammate: NSSet?

    public var wrappedTeamName: String {
        teamName ?? "Unkown team"
    }

    public var teammateArray: [Teammate] {
        let set = teammate as? Set<Teammate> ?? []
        return set.sorted {
            $0.wrappedLastName < $1.wrappedLastName
        }
    }
}

3      

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.