I am trying to create view model according to JSON response but getting bellow error.
import Foundation
import SwiftUI
public class DeclarationViewModel: ObservableObject {
@Published var description: [DeclarationListViewModel]?
init() {
self.description = [DeclarationListViewModel]()
}
init(shortDescription: [DeclarationListViewModel]?) {
self.description = shortDescription
}
}
public class DeclarationListViewModel: ObservableObject, Hashable {
@Published var yesNo: Bool?
@Published var title: String?
}
trying to use result in foreach
Thank you for help. Please let me know if more details are required.
Remove the import SwiftUI
try not to use it in your ViewModels, unless really necessary. Also remove Hashable
from your class declaration and outside of it create an extension like this for example:
extension DeclarationListViewModel: Identifiable, Hashable {
var identifier: String {
return UUID().uuidString
}
public func hash(into hasher: inout Hasher) {
return hasher.combine(identifier)
}
public static func == (lhs: DeclarationListViewModel, rhs: DeclarationListViewModel) -> Bool {
return lhs.identifier == rhs.identifier
}
}
Also remember that structs
exists, they are great for defining your models.
One more thing, maybe instead of having an optional boolean, why not initialize it with false and in your view or viewmodel, wherever you call it first, set it to true if that's the case.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With