0
votes

Write is saving/writing model and EmployeeType enum to file, but getting nil when reading. I think the error is in the decoding extension of the EmployeeType enum? I added the print URLs to verify the write and read directory & files are the same. The third print is to show that the returned "decodedEmployees" coming from the read/decode method readEmployeesFromFile() is returning nil meaning the error is in "let decodedEmployees = try? ..."

import UIKit
struct Employee: Codable {

    var name: String
    var dateOfBirth: Date
    var employeeType: EmployeeType
}

enum EmployeeType: Codable {
    case exempt
    case nonExempt
    case partTime

    static let all: [EmployeeType] = [.exempt, .nonExempt, .partTime]

    func description() -> String {
        switch self {
        case .exempt:
            return "Exempt Full Time"
        case .nonExempt:
            return "Non-exempt Full Time"
        case .partTime:
            return "Part Time"
        }
    }
}

extension EmployeeType {
    init(from decoder: Decoder) throws {
        var container = try decoder.unkeyedContainer()
        let employeeType = try container.decode(EmployeeType.self)
        self = employeeType

        return
        }
     func encode(to encoder: Encoder) throws {
        var container = encoder.unkeyedContainer()
        try container.encode(description())
        return
    }
}

var count = 0
 func readEmployeeFromFile()->[Employee] {
       let documentsDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
       let archiveURL = documentsDirectory.appendingPathComponent("Employees").appendingPathExtension("plist")
       let employeeDecoder = PropertyListDecoder()
            print("Read: \(archiveURL)")
    if let employeesDataFromFile = try? Data(contentsOf: archiveURL) {
        let decodedEmployees = try? employeeDecoder.decode(Array<Employee>.self, from: employeesDataFromFile)
        print("failed read if nil: \(decodedEmployees)")
    }
    return[Employee(name: "Fail", dateOfBirth: Date(), employeeType: .partTime)]
}

    func writeEmployeeToFile(newEmployeesToSave: [Employee]){
        let documentsDirectory =
            FileManager.default.urls(for: .documentDirectory,
                in: .userDomainMask).first!
            let archiveURL =
            documentsDirectory.appendingPathComponent("Employees")
               .appendingPathExtension("plist")
            print("Write: \(archiveURL)")
        let propertyListEncoder = PropertyListEncoder()
        let savedEmployees = try? propertyListEncoder.encode(newEmployeesToSave)
        try? savedEmployees!.write(to: archiveURL, options: .noFileProtection)
    }

let emps = [Employee(name: "A", dateOfBirth: Date(), employeeType: .exempt)]
writeEmployeeToFile(newEmployeesToSave: emps)
let readEmps = readEmployeeFromFile()
print("read: \(readEmps)")

1

1 Answers

1
votes

You are making it too difficult:

enum EmployeeType: String, Codable, CaseIterable {
  case exempt = "Exempt Full Time"
  case nonExempt = "Non-exempt Full Time"
  case partTime = "Part Time"
}

Everything else will work. allCases property is generated automatically.

If you really wanted to do the encoding by yourself, you should look into singleValueContainer instead of unkeyedContainer and use init(rawValue:) for initialization. Unkeyed container is basically an array.