我想你需要这个:
import SwiftUI
struct MyUserData
{
var userId: String
var userFirstName: String
var userLastName: String
}
struct ContentView: View {
@State var arrayOfMyUserData: [MyUserData] = [MyUserData]()
var body: some View {
List
{
ForEach(arrayOfMyUserData, id: \.userId) { item in
Text("FirstName: " + item.userFirstName) + Text(" LastName: " + item.userLastName)
}
}
.onAppear() {
arrayOfMyUserData = [MyUserData(userId: "userId1", userFirstName: "willy", userLastName: "will"),
MyUserData(userId: "userId2", userFirstName: "bob", userLastName: "mccarthy"),
MyUserData(userId: "userId3", userFirstName: "james", userLastName: "rodriguez")
]
}
}
}
或者你可以使用这个:
struct MyUserData
{
let userId: UUID = UUID()
var userFirstName: String
var userLastName: String
}