0

我有一个模型,我有一个甲板变量。现在我想从 deck array 中删除十二个项目。但是,我删除的每个项目的模型都在发生变化,我希望仅在移除十二张卡片后才能更改视图。函数 getTwelveCards 正在连续运行并且所有卡片都被移除。所需的功能是只删除十二张卡片这是我的模型:

struct SetGame<Color, Shape, Shadings> {
    
    var deck : Array<SetCard>
    var addedTwelveCards = false
    
    func printCardCount() {
        print("card count: \(deck.count)")
    }
    
    mutating func getASetCard() -> SetCard {
            deck.remove(at: 0)
        
    }
    
    mutating func getTwelveCards() -> Array<SetGame<Color, Shape, Shadings>.SetCard> {
        var cards : Array<SetGame<Color, Shape, Shadings>.SetCard> = []
        if deck.count >= 12 && !addedTwelveCards{
            for _ in 1...12 {
                cards.append(getASetCard())
            }
        }
        addedTwelveCards = true
        return cards
    }
    
    
    struct SetCard : Identifiable {
        var color : Color
        var shapeName : ShapeName
        var shading : Shadings
        var count : Int
        var id = UUID()
        
    }
}

这是我的视图模型:

class SetGameViewModel: ObservableObject {
    
    @Published private var setGameModel = SetGame<Color, ShapeName, Shadings>(deck: createSetGameDeck())
    
    
    static let colors = [Color.red, Color.green, Color.blue]
    static let shapes = [ShapeName.Capsule, ShapeName.Circle, ShapeName.Square]
    static let shadings = [Shadings.fill, Shadings.striped, Shadings.stroked]
    
    
     
    static  func createSetGameDeck() -> Array<SetGame<Color, ShapeName, Shadings>.SetCard> {
        var deck : Array<SetGame<Color, ShapeName, Shadings>.SetCard> = []
        
            for color in colors {
                for shape in shapes {
                    for shading in shadings {
                        for count in 1...3 {
                            let setCard = SetGame<Color, ShapeName, Shadings>.SetCard(color: color, shapeName: shape, shading: shading, count: count)
                            deck.append(setCard)
                        }
                        
                    }
                }
            }
          deck.shuffle()
        print("\(deck.count)")
          return deck
        }
        
    // MARK: Intents
    
    func getCardCount() {
        setGameModel.printCardCount()
    }
    
    var deck : Array<SetGame<Color, ShapeName, Shadings>.SetCard> {
        get {
            
            setGameModel.deck
        }
    }
    
    var addedTwelveCards : Bool {
        self.setGameModel.addedTwelveCards
    }
    
    var getFirstTwelveSetCards : Array<SetGame<Color, ShapeName, Shadings>.SetCard> {
        print("new deck count: \(self.setGameModel.deck.count)")
        return self.setGameModel.getTwelveCards()
      
    }
    
    
}

这是我的观点:

struct SetGameView: View {
    
    @ObservedObject var setGameViewModel : SetGameViewModel
    
    var body: some View {
        Group {
            if !setGameViewModel.addedTwelveCards {
            Grid(self.setGameViewModel.getFirstTwelveSetCards) { setCard in
                       SetCardView(shapeName: setCard.shapeName, shading: setCard.shading, color: setCard.color, count: setCard.count)
                   }
            }
        }
       
        
    }
}
4

0 回答 0