title: enum encoded value is nil while storing the class object in UserDefaults. Codable Protocol is already inherited
body: <p>I am new to iOS and trying to store User object in UserDefaults. So that when the app is launched again, I can check user type and based on it, I need to navigate to relevant screen.</p>
  <p>For that, I have created a User class as below (Codable) and it has one userType enum property!</p>
  <pre><code>enum UserType: Int, Codable {
      case userType1 = 0 
      case userType2 = 1
      case notDetermined = 2
      
      init(from decoder: Decoder) throws {
          let label = try decoder.singleValueContainer().decode(Int.self)
          self = UserType(rawValue: label) ?? .notDetermined
      }
  }
  
  class User: Codable {
      public var userFullName: String? = &quot;&quot;
      public var userType: UserType? //= .notDetermined
      
      enum CodingKeys: String, CodingKey {
          case userFullName
      }
  }
  
  </code></pre>
  <p>In my view Controller class, I am creating a new instance for User object and trying to store in user defaults as below:</p>
  <pre><code>        let newUser = User()
          newUser.userFullName = &quot;Test&quot;
          newUser.userType = userTypeBtn.isSelected ? .userType1 : .userType2
          
  </code></pre>
  <p>when I print the newUser's userType, I can see proper value whichever is selected. But after that, when I am trying to store it in userDefaults as below, it returns nil for userType property.</p>
  <pre><code>            do {
                  let encoded = try JSONEncoder().encode(newValue)
                  UserDefaults.standard.set(encoded, forKey: UserDefaultKey.currentUser)
                  UserDefaults.standard.sync()
              } catch {
                  print(&quot;Unable to Encode User Object: (\(error))&quot;)
              }
  
  </code></pre>
  <p>when I tried to print this <strong>encoded</strong> variable, and decoded it in console</p>
  <pre><code>JSONDecoder().decode(User.self, from: encoded).userType
  </code></pre>
  <p>it prints nil.</p>
  <p>Please help me how can I store optional enum property in UserDefaults and retrieve it when needed using Codable</p>