1

我对编程很陌生,我在更复杂的方面遇到了麻烦。我正在尝试使用一个名为 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()

下面是我想从添加到我的 playerCards 数组的最后一个元素中显示名称的地方,例如“name1”。我将来可能希望将此图像制作为图像并使用 ID: 来显示相应的图像,但我无法访问数组中的类型

          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)
                                    }
                            }

                    }
            }
        }
    }
}
4

1 回答 1

1

这是在 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")
 }
于 2020-06-09T05:35:57.587 回答