I am trying to make an enum ParentalSalary conform to Codable so that it can be saved in UserDefaults. I have tried to follow this guide: https://blog.natanrolnik.me/codable-enums-associated-values and tweaked it according to my program.
Furthermore, I've gotten stuck in the trouble shooting as I can't find any solutions / explanations when searching for the error message.
Below is the code for the enum I'm trying to make Codable:
enum ParentalSalary {
case noSalary
case upTo(percentage: Int)
}
extension ParentalSalary : Codable {
private enum CodingKeys: String, CodingKey {
case typeOfSalary
case percentage
}
private enum TypeOfSalaryCodingKeys : String, Codable {
case noSalary
case upTo
}
func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
switch self {
case .noSalary:
try container.encode(TypeOfSalaryCodingKeys.noSalary, forKey: .typeOfSalary)
case .upTo(let percentage):
try container.encode(TypeOfSalaryCodingKeys.upTo, forKey: .typeOfSalary)
try container.encode(percentage, forKey: .percentage)
}
}
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
let typeOfSalary = try container.decode(ParentalSalary.self, forKey: .typeOfSalary)
switch typeOfSalary {
case .noSalary:
self = .noSalary
case .upTo:
let percentage = try container.decode(Int.self, forKey: .percentage)
self = .upTo(percentage: percentage)
}
}
}
Furthermore, I use the ParentalSalary data type in a separate struct (Profile: Identifiable, Codable) which I in turn make an ObservableObject and save to UserDefaults using a JSONEncoder() and JSONDecoder(). See below:
class TheProfile: ObservableObject {
@Published var profile = Profile.default{
didSet {
let encoder = JSONEncoder()
if let encoded = try? encoder.encode(profile) {
UserDefaults.standard.set(encoded, forKey: "Profile")
}
}
}
init() {
if let profile = UserDefaults.standard.data(forKey: "Profile") {
let decoder = JSONDecoder()
if let decoded = try? decoder.decode(Profile.self, from: profile) {
self.profile = decoded
return
}
}
self.profile = Profile.default
}
}
I get the error message below (in a separate SwiftUI view for unrelated code written earlier which did not give an error previously):
Generic parameter 'S' could not be inferred
Am I making a misstake in the ParentalSalary enum in the sense that it does not conform to Codable or is something else causing this error?
One suspicion I had (with respect to me just learning this) is that there could be an issue with encoding the Profile: Identifiable, Codable with a JSONEncoder() combined with encoding the ParentalSalary enum with a different (?) encoder.
Any input or support is greatly appreciated.
ParentalSalaryPicker(selection: $profile.numberOfParents, label: Text("XXX")) { ForEach(Profile.numberOfParents.allCases, id: \.self) {p in Text(p.rawValue).tag(p) } }{it should be good). Then, see which line Xcode puts the error on. If the issue doesn't suddenly jump out at you add the troublesome code to your question with some context. Add a comment to the end of the line that has the error indicating that that is the specific error location. This will allow people to see the error in context which will get you better answers.Array()Xcode would be lost but if you usedArray<String>()it knows exactly what you mean (note thatArray<String>()is the same as[String]()).