0

I am struggling to work how I can use a protocol to represent the core data type, SectionFetchedResults to enable me to add snapshot testing for my iOS SwiftUI app.

Below is the code and I am trying to pass players into SquadView using a protocol to represent it.

What do you think I could do?

import SwiftUI

struct SquadContainerView: View {
    @Environment(\.managedObjectContext) var viewContext
    @SectionedFetchRequest var players: SectionedFetchResults<Int16, Player>
    private let team: Teamable
    
    init(team: Teamable) {
        let predicate =  NSPredicate(format: "%K == %@", #keyPath(Player.team.id), team.id as CVarArg)
        _players = SectionedFetchRequest<Int16, Player>(sectionIdentifier: \.position, sortDescriptors: [SortDescriptor(\.shirtNumber)], predicate: predicate)
        self.team = team
    }
    
    var body: some View {
        SquadView(viewModel: .init(team: team,
                                   playerService: PlayerService(viewContext),
                                   teamService: TeamService(viewContext)),
                  players: players)
    }
}
Edward
  • 2,864
  • 2
  • 29
  • 39
  • 1
    Does this answer your question https://stackoverflow.com/a/61495490/12299030? – Asperi Jul 05 '22 at 03:19
  • Hi @Asperi thanks again for helping out, Unfortunately I get this error when trying that `generic struct 'SquadView' requires the types 'SectionedFetchResults.Element' (aka 'SectionedFetchResults.Section') and 'Player' be equivalent` I did look at that answer which I have used in another part of my code. – Edward Jul 05 '22 at 05:26
  • The signature of my struct was `struct SquadView: View where Results.Element == Player` – Edward Jul 05 '22 at 05:28
  • take a look at this general example https://stackoverflow.com/a/71032848/14017100 – ARCHER Jul 14 '22 at 20:17

0 Answers0