Post

Replies

Boosts

Views

Activity

SwiftData @Model Codable conformance for array of structs
I have a struct that is identifiable, and has two String variables and a UUID. I am using @Model decorator/macro for a class I want to persist, which has a few String variables (with values assigned to them), and some arrays of my struct, which are assigned values with the init() initialiser. This class also has two functions, both of which return an integer. I am getting many errors for the String variables and initialisation of the arrays of structs: Referencing instance method 'setValue(forKey:to:)' on 'Array' requires that 'Record' conform to 'PersistentModel' No exact matches in call to instance method 'setValue' No exact matches in call to instance method 'getValue' I tried converting the struct to class, but that did not help. I also tried writing my own encoder and decoder functions, but that did not help either. // Created by Devansh Trivedi on 24/11/23. // import SwiftUI // For UUID() to make Record struct Identifiable import SwiftData struct Record : Identifiable { var date: String var value: String var id = UUID() } @Model class Card { //https://www.donnywals.com/making-your-swiftdata-models-codable/ // enum CodingKeys: CodingKey { // case unit, date, name // } // required init(from decoder: Decoder) throws { // let container = try decoder.container(keyedBy: CodingKeys.self) // self.unit = try container.decode(String.self, forKey: .unit) // self.date = try container.decode(String.self, forKey: .date) // self.name = try container.decode(String.self, forKey: .name) // } // func encode(to encoder: Encoder) throws { // var container = encoder.container(keyedBy: CodingKeys.self) // try container.encode(unit, forKey: .unit) // try container.encode(date, forKey: .date) // try container.encode(date, forKey: .name) // } var unit:String="Liters" var date:String="Nov 23" var name:String="Milk" var column_1:[Record] var column_2:[Record] var column_3:[Record] init() { self.column_1 = [ Record(date: "1", value: ""), Record(date: "2", value: ""), Record(date: "3", value: ""), Record(date: "4", value: ""), Record(date: "5", value: ""), Record(date: "6", value: ""), Record(date: "7", value: ""), Record(date: "8", value: ""), Record(date: "9", value: ""), Record(date: "10", value: "") ] self.column_2 = [ Record(date: "11", value: ""), Record(date: "12", value: ""), Record(date: "13", value: ""), Record(date: "14", value: ""), Record(date: "15", value: ""), Record(date: "16", value: ""), Record(date: "17", value: ""), Record(date: "18", value: ""), Record(date: "19", value: ""), Record(date: "20", value: "") ] self.column_3 = [ Record(date: "21", value: ""), Record(date: "22", value: ""), Record(date: "23", value: ""), Record(date: "24", value: ""), Record(date: "25", value: ""), Record(date: "26", value: ""), Record(date: "27", value: ""), Record(date: "28", value: ""), Record(date: "29", value: ""), Record(date: "30", value: ""), Record(date: "31", value: "") ] } func total() -> Int { // https://hoyelam.com/summing-up-numeric-properties-of-objects-in-an-array-in-swift/ // https://dev.to/hoyelam/summing-up-numeric-properties-of-objects-in-an-array-in-swift-4og return column_1.lazy .filter( { !$0.value.isEmpty } ) .map( { Int($0.value)! } ) .reduce(0, +) + column_2.lazy .filter( { !$0.value.isEmpty } ) .map( {Int($0.value)! }) .reduce(0, +) + column_3.lazy .filter({!$0.value.isEmpty}) .map( {Int($0.value)! }) .reduce(0, +) } func bill() -> Int { return self.total()*65 } }
2
2
1.6k
Nov ’23
Trying to connect model in SwiftData with SwiftUI TextField
I am trying to update values of SwiftData model by letting user change string inside a TextField. But the binding variable is overwriting the value in TextField instead of querying and showing the value from SwiftData model. I am using @Query in the view and have a @State variable for TextField for Binding variable, initialised to 0 at its definition. So I see 0 in the TextField. But I want to see the value from SwiftData in the TextField instead. How can I do that?
1
0
1.6k
Nov ’23