在使用EnvironmentObject
并在详细视图中将变量绑定到我的数据时,我遇到了一些麻烦。我相信真理之源是@EnvironmentObject var userData: UserData
。在列表视图中点击一行时,该详细信息视图应显示所选项目,并且您应该能够通过weight.weightdate
来修改DatePicker
。应用程序会编译,当我使用下面的代码时,但是当我尝试使用类似DatePicker(selection: $userData.weights[weightIndex].day, in: ...Date(), displayedComponents: [.date,.hourAndMinute]) {
之类的东西绑定到我的数据时,出现以下错误:
Type 'Any' has no member 'date'
Type 'Any' has no member 'hourAndMinute'
如果我添加 DatePickerComponents .date,我会在Text("Date")
行上得到表达式类型不明确而没有更多上下文。
struct Weight: Hashable, Codable, Identifiable {
var id: Int
var weightdate: Date
var weight: Float = 0
...
}
struct WeightDetail: View {
@EnvironmentObject var userData: UserData
var weight: Weight
var weightIndex: Int {
userData.weights.firstIndex(where: { $0.id == weight.id })!
}
@State private var selectedDay = Date()
var body: some View {
Form {
Section {
//DatePicker(selection: $userData.weights[weightIndex].day, in: ...Date(), displayedComponents: [.date,.hourAndMinute]) {
//ERROR Type 'Any' has no member 'date'
//ERROR Type 'Any' has no member 'hourAndMinute'
DatePicker(selection: $selectedDay, in: ...Date(), displayedComponents: [.date,.hourAndMinute]) {
Text("Date")
//also works: Text("\(weight.weightdate)")
}
}
...
答案 0 :(得分:0)
我对数据做了一些假设。看现场的代表。它应该提供有关环境对象的一些想法。
import SwiftUI
struct ContentView: View {
var body: some View {
WeightDetail(weight: Weight(id: 0, weightdate: Date()))
}
}
struct WeightDetail: View {
@EnvironmentObject var userData: UserData
var weight: Weight
var weightIndex: Int {
userData.weights.firstIndex(where: { $0.id == weight.id })!
}
@State private var selectedDay = Date()
var body: some View {
Form {
Section {
// DatePicker(selection: $userData.weights[weightIndex].day, in: ...Date(), displayedComponents: [.date,.hourAndMinute])
DatePicker(selection: $userData.weights[weightIndex].weightdate, in: ...Date(), displayedComponents: [.date, .hourAndMinute] ) {
Text("Display")
}
//{
//ERROR Type 'Any' has no member 'date'
//ERROR Type 'Any' has no member 'hourAndMinute'
// DatePicker(selection: $selectedDay, in: ...Date(), displayedComponents: [.date,.hourAndMinute]) {
// Text("Date")
// //also works: Text("\(weight.weightdate)")
// }
}
}
}
}
struct Weight: Hashable, Codable, Identifiable {
var id: Int
var weightdate: Date
var weight: Float = 0
}
class UserData: ObservableObject {
@Published var weights = [Weight]()
init(weights: [Weight]){
self.weights = weights
}
}
SceneDelegate:
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
var window: UIWindow?
var userData: UserData!
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
let weight = Weight(id: 0, weightdate: Date().advanced(by: -100000))
let weights = [weight]
userData = UserData(weights: weights)
let contentView = ContentView().environmentObject(userData)
// Use a UIHostingController as window root view controller.
if let windowScene = scene as? UIWindowScene {
let window = UIWindow(windowScene: windowScene)
window.rootViewController = UIHostingController(rootView: contentView)
self.window = window
window.makeKeyAndVisible()
}
}
}