[英]How Do I Display A List Of Values From An Array Of A Customer Type From Struct [Swift]
In ContentView: View
I have an array of days with some data attached to it from HealthKit.在ContentView: View
中,我有一连串的日子,其中包含一些来自 HealthKit 的数据。 The day is unique.这一天是独一无二的。 The array is printing fine and everything to do that with that is correctly populated.该数组打印正常,并且正确填充了与之相关的所有内容。
//Prep workouts to display
public struct workoutHistory {
var workoutDate: Date
var strengthCount: Int
var cardioCount: Int
var flexibilityCount: Int
}
@State public var workoutHistoryByDay: [workoutHistory] = []
Then in the body I try to display every date in a list (just to test at the moment)然后在正文中,我尝试在列表中显示每个日期(目前只是为了测试)
ForEach(workoutHistoryByDay) { workoutDay in
Text("\(workoutDay.workoutDate)")
Text("\(workoutDay.strengthCount)")
}
When I do this I get the error:当我这样做时,我得到了错误:
Referencing initializer 'init(_:content:)' on 'ForEach' requires that 'ContentView.workoutHistory' conform to 'Identifiable'在 'ForEach' 上引用初始化程序 'init(_:content:)' 要求 'ContentView.workoutHistory' 符合 'Identifiable'
I have tried updating my struct to include : Identifiable
and I get the error我尝试更新我的结构以包括: Identifiable
,我收到错误
Type 'ContentView.workoutHistory' does not conform to protocol 'Identifiable'类型“ContentView.workoutHistory”不符合协议“可识别”
I have also tried updating the ForEach to include ID我还尝试更新 ForEach 以包含 ID
ForEach(workoutHistoryByDay, id: .workoutDate) { workoutDay in
Text("\(workoutDay.workoutDate)")
Text("\(workoutDay.strengthCount)")
}
And I get the error我得到了错误
Generic parameter 'ID' could not be inferred无法推断通用参数“ID”
My question is, how to I properly list every .workoutDate
and .strengthCount
in my array workoutHistoryByDay
?我的问题是,如何正确列出数组workoutHistoryByDay
的每个.workoutDate
和.strengthCount
? Thank you.谢谢你。
try this approach, as in this sample code, works very well for me:尝试这种方法,就像在这个示例代码中一样,对我来说效果很好:
struct ContentView: View {
// -- here some test workout
@State public var workoutHistory: [Workout] = [
Workout(workoutDate: Date(), strengthCount: 1, cardioCount: 2, flexibilityCount: 3),
Workout(workoutDate: Date(timeIntervalSinceNow: Double(86400 * 1)), strengthCount: 2, cardioCount: 2, flexibilityCount: 3),
Workout(workoutDate: Date(timeIntervalSinceNow: Double(86400 * 2)), strengthCount: 3, cardioCount: 2, flexibilityCount: 3)]
var body: some View {
List {
ForEach(workoutHistory) { workout in
Text("\(workout.workoutDate)")
Text("\(workout.strengthCount)")
}
}
}
}
public struct Workout: Identifiable { // <-- here
public let id = UUID() // <-- here
var workoutDate: Date
var strengthCount: Int
var cardioCount: Int
var flexibilityCount: Int
}
You would gain a lot by doing the tutorial again at: https://developer.apple.com/tutorials/swiftui/通过在以下位置再次学习本教程,您会收获很多: https://developer.apple.com/tutorials/swiftui/
You have to inherit Identifiable
protocol to your struct model and need to confirm ID.您必须将Identifiable
协议继承到您的结构 model 并且需要确认 ID。
public struct workoutHistory: Identifiable { //<== here
public var id: Date { //<== here
self.workoutDate
}
var workoutDate: Date
var strengthCount: Int
var cardioCount: Int
var flexibilityCount: Int
}
you can return workoutDate
as an id if the date is unique else you have to set a unique id.如果日期是唯一的,您可以将workoutDate
日期作为 ID 返回,否则您必须设置唯一的 ID。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.