Hello
Is there any way to put the 2 Texts in an HStack without putting the 2 ForEachs together?
Code:
Thank you
Is there any way to put the 2 Texts in an HStack without putting the 2 ForEachs together?
Code:
Code Block ForEach(model.data, id: \.objectID){ obj in Text(model.getValue(obj: obj)) }.onDelete(perform: model.deleteData) ForEach(date.data, id: \.objectID){ obj1 in Text("\(date.getValue(obj: obj1), formatter: dateFormatter)") }.onDelete(perform: date.deleteData)
Thank you
Define only one Entity CombinedContent with two Attributes in your xcdatamodel:What do you mean with combined entity and how do I do it?
value of type String
date of type Date
Code Block class DataModel: ObservableObject{ @Published var data: [CombinedContent] = [] var txt = "" var dataAttuale = Date() let context = persistentContainer.viewContext init() { readData() } func readData(){ let request: NSFetchRequest<CombinedContent> = CombinedContent.fetchRequest() do { let results = try context.fetch(request) self.data = results } catch { print(error.localizedDescription) } } func writeData(){ let entity = NSEntityDescription.insertNewObject(forEntityName: "CombinedContent", into: context) as! CombinedContent entity.value = txt entity.date = dataAttuale do { try context.save() self.data.append(entity) } catch { print(error.localizedDescription) } } func deleteData(indexSet: IndexSet){ for index in indexSet{ do { let obj = data[index] context.delete(obj) try context.save() let index = data.firstIndex(of: obj) data.remove(at: index!) } catch { print(error.localizedDescription) } } } func updateData(){ } }
You can use it as:
Code Block ForEach(combinedModel.data, id: \.objectID){ obj in HStack { Text(obj.value) Text("\(obj.date, formatter: dateFormatter)") } }.onDelete(perform: combinedModel.deleteData)