SwiftUI:我想使用数组中的信息,但找不到正确的命令

SwiftUI: I want to use the information in my array but I'm unable to find the right command

我对编程还很陌生,在处理更复杂的方面时遇到了麻烦。我正在尝试使用一个名为 playerCameCards 的数据集,并将其中的 5 个随机添加到 playerCards。我已经设法做到了,但我想显示名称:最后添加的元素,但我不知道该怎么做。我会很感激一些帮助下面是我的代码。

import Foundation
import SwiftUI

class Cards: ObservableObject {

    @Published var playerCards = [gameCards]()
    @Published var playerGameCards = [
        gameCards(id: 1, name: "name1", attack: 92, defence: 49, gameControl: 60, creativity: 72, legend: 4),
        gameCards(id: 2, name: "name2", attack: 87, defence: 40, gameControl: 65, creativity: 80, legend: 2),
        gameCards(id: 3, name: "name3", attack: 43, defence: 93, gameControl: 40, creativity: 45, legend: 3),
        gameCards(id: 4, name: "name4", attack: 88, defence: 51, gameControl: 80, creativity: 92, legend: 5),
        gameCards(id: 5, name: "name5", attack: 85, defence: 51, gameControl: 72, creativity: 81, legend: 3),
        gameCards(id: 6, name: "name6", attack: 91, defence: 38, gameControl: 72, creativity: 89, legend: 5),
        gameCards(id: 7, name: "name7", attack: 34, defence: 95, gameControl: 40, creativity: 50, legend: 5),
        gameCards(id: 8, name: "name8", attack: 86, defence: 63, gameControl: 89, creativity: 84, legend: 4),
        gameCards(id: 9, name: "name9", attack: 90, defence: 30, gameControl: 50, creativity: 83, legend: 5),
        gameCards(id: 10, name: "name10", attack: 32, defence: 92, gameControl: 42, creativity: 32, legend: 4)
    ]
}
struct gameCards: Identifiable {

    let id: Int
    let name: String
    let attack: Int
    let defence: Int
    let gameControl: Int
    let creativity: Int
    let legend: Int



}





import SwiftUI

struct ContentView: View {
    @EnvironmentObject var cardData: Cards
    @State var firstTime: Bool = false
    @State var currentCard: String = "Back"

    var body: some View {

        ZStack{

            VStack{

                Spacer()

                Text("Lets open your 1st cards").background(Color.white).font(.title)

                Spacer()

Below is where I want show the name e.g. "name1" from the last element added to my playerCards array. I may want to make this an image in the future and use the ID: to display the corresponding image but I'm unable to access the types in my array

          Text("I want to show the "name" of the element in the last array I've just used").frame(width: 200, height: 200).background(Color.blue)

                Spacer()

                Button(action:{
                            if self.cardData.playerCards.count <= 4{
                                self.cardData.playerCards.append(self.cardData.playerGameCards.randomElement()!)


                                print("\([self.cardData.playerCards.last])")

                            }

                            if self.cardData.playerCards.count > 4 {
                                self.firstTime = true
                            }

                    }){

                            if self.cardData.playerCards.count <= 4 {
                            Image("Click").renderingMode(.original).resizable().frame(width: 100, height: 100)


                            }
                            else{
                                NavigationLink(destination: ContentView()){Image("Continue").renderingMode(.original).resizable().frame(width: 100, height: 100)
                                    }
                            }

                    }
            }
        }
    }
}

这是在 Swift 中的字符串中嵌入值的方法:

if self.cardData.playerCards.last != nil {
     Text("I want to show the \(self.cardData.playerCards.last!.name) of the element in the last array I've just used")
 }