I am tinkering around with URLSession and was essentially building a super simple app to load a JSON file into ContentView sort of like a friends list from Facebook and wanted some clarity on not any errors I'm having, but rather, the internal workings of Swift's Codable
protocol. Here are some code and explanations:
struct User: Identifiable, Codable {
struct Friend : Identifiable, Codable {
var name : String
var id : String
}
var id : String
var isActive: Bool
var name : String
var age: Int
var company : String
var address : String
var about : String
var registered : String
var friends : [Friend]
var checkIsActive: String {
return self.isActive ? "" :""
}
}
So to summarize above, I have a User
struct which contains a bunch of properties that conform to Codable
.
class UsersArrayClass: ObservableObject {
@Published var userArray = [User]()
}
However, I have another class, UsersArrayClass
which creates a @Published var userArray
of User
struct objects. This class conforms to the @ObservableObject
protocol, but of course, when I try to make it conform to Codable
, it does not like that likely because of the @Published
property wrapper being applied on the array itself... This is what is essentially confusing me though if the User struct has Codable
conformance, why doesn't userArray
which contains User
objects automatically conform to Codable
as well?
I was thinking that maybe loading all of this up into a Core Data model would solve my problems, but I still can't move on unless I understand what I'm missing out on here, so thanks in advance for any input.
Published don't conform to Codable or any common protocol in Foundation currently
Trying to make
Published
conform toCodeable
resulting error below: