Hello,
I don't know why I'm getting the error "Failed to produce diagnostic for expression", I have use the List before on a similar way and can't find the error.
I'll appreciate some help
Working with Xcode 12.5.1
If I Change the List to this it works:
List(1 ..< 6) { mood in
HStack {
Text("Hola")
.frame(width: 200, height: 50, alignment: .leading)
}
}
MoodScreen.swift
import SwiftUI
struct MoodScreen: View {
@State private var addMoodModal = false
var body: some View {
VStack {
VStack {
MoodView(addMoodModal: $addMoodModal)
.sheet(isPresented: $addMoodModal, content: {
AddNewMoodScreen(addMoodModal: $addMoodModal)
})
MoodListView()
Spacer()
}
}
.navigationTitle("Mood summary")
.embedInNavigationView()
}
}
struct MoodScreen_Previews: PreviewProvider {
static var previews: some View {
MoodScreen()
}
}
MoodListView.swift
import SwiftUI
struct MoodListView: View {
@ObservedObject var moodsScreen = MoodsViewModel()
var body: some View { //Error is shown here
VStack{
List(moodsScreen.moodsVM) {mood in
HStack {
Text("Hola")
.frame(width: 200, height: 50, alignment: .leading)
}
}
}
}
}
MoodViewModel.swift
import SwiftUI
class MoodsViewModel: ObservableObject {
@ObservedObject private var moods = MoodDataBase()
@Published var moodsVM = [MoodViewModel]()
func callMoods() {
DispatchQueue.main.async {
self.moodsVM = self.moods.moodDatabase.map(MoodViewModel.init)
}
}
}
class MoodViewModel {
var mood: MoodModel
init(mood: MoodModel) {
self.mood = mood
}
var moodId: String {
mood.id.uuidString
}
var moodLevel: String {
mood.value.moodLevel
}
var moodDate: String {
"01/01/2021"
}
}
MoodsDataBase.swift
import Foundation
class MoodDataBase: ObservableObject {
@Published var moodDatabase: [MoodModel] = [MoodModel]()
init() { }
static let shared = MoodDataBase()
// Calendar and Date
let userCalendar = Calendar.current
let calendarComponents: Set<Calendar.Component> = [
.year,
.month,
.day,
.hour,
.minute,
.second
]
}
extension MoodDataBase {
func AddMood(newLevel: MoodValue) -> Void {
let newMood: MoodModel = MoodModel(id: UUID(), date: today(), value: newLevel)
DispatchQueue.main.async {
self.moodDatabase.append(newMood)
print("New Mood to add: \(newMood)")
print("Mood Count: \(self.moodDatabase.count)")
}
}
func today() -> DateComponents {
let today = userCalendar.dateComponents(calendarComponents, from: Date())
return today
}
}