Purpose
I have a User class with a status property and I want store it into NSUserDefault which need to encode the User class first.
User class code updated version
public class User: NSObject, NSCoding {
override init() {}
var status: Status = .unKnow
required convenience public init(coder aDecoder: NSCoder) {
self.init()
self.status = aDecoder.decodeObject(forKey: "UserStatus") as? Status ?? .unKnow
}
public func encode(with aCoder: NSCoder) {
aCoder.encode(self.status, forKey: "UserStatus")
}
public func updatePersistentData() {
let userDefault = UserDefaults.standard
/// stuck at this line
let encodedData: Data = NSKeyedArchiver.archivedData(withRootObject: self)
userDefault.set(encodedData, forKey: "User")
userDefault.synchronize()
}
}
And my nested enumeration
enum Status {
/// is Login status
case isLogin(style: LoginStatus)
/// is register
case isRegister(style: RegisterStatus)
/// is fetch user info
case isGetUserInfo(style: GetUserInfoStatus)
/// nonabove
case unKnow
}
enum LoginStatus: Int {
case a
case b
case c
case d
}
enum RegisterStatus: Int {
case a
case b
case c
case d
}
enum GetUserInfoStatus: Int {
case a
case b
case c
case d
}
I found this, and understand I need to convert enum into raw value. It seems need to use a normal enum with string-style or Int-style...etc.
And I run the code, error message
HJC[2371:403228] -[_SwiftValue encodeWithCoder:]: unrecognized selector sent to instance 0x7f940e6bec30
HJC[2371:403228] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[_SwiftValue encodeWithCoder:]: unrecognized selector sent to instance 0x7f940e6bec30'
I tried to encode and decode the status since it's the only property in the class but you might need to do the same for the other properties if any was found
First i started with giving the State enum a value that i can encode
enum Status {
enum StatusValue {
case isLogin(LoginStatus)
case isRegister(RegisterStatus)
case isGetUserInfo(RegisterStatus)
case unknow
}
}
extension Status.StatusValue {
var value: Int {
switch self {
case .isLogin(let value):
return value.rawValue
case .isRegister(let value):
return value.rawValue
case .isGetUserInfo(let value):
return value.rawValue
case .unknow:
return -1
}
}
}
enum LoginStatus: Int {
case a = 0
case b
case c
case d
}
enum RegisterStatus: Int {
case a = 4
case b
case c
case d
}
enum GetUserInfoStatus: Int {
case a = 8
case b
case c
case d
}
Second I configured the User class to implement NSCoding
public class User: NSObject, NSCoding {
override init() {
status = .unknow
}
init(_ status: Status.StatusValue) {
self.status = status
}
var status: Status.StatusValue
public func encode(with aCoder: NSCoder) {
print(self.status.value)
aCoder.encode(self.status.value, forKey: "status")
}
public required convenience init(coder aDecoder: NSCoder) {
let status = aDecoder.decodeObject(forKey: "status") as? Status.StatusValue ?? .unknow
self.init(status)
}
func save() {
let savedData = NSKeyedArchiver.archivedData(withRootObject: self)
let defaults = UserDefaults.standard
defaults.set(savedData, forKey: "user")
defaults.synchronize()
}
}
Finally I tested the result through
let user1: User = User()
user1.status = .isLogin(LoginStatus.b)
user1.save()
let user2: User
let defaults = UserDefaults.standard
if let saveduser = defaults.object(forKey: "user") as? Data {
user2 = NSKeyedUnarchiver.unarchiveObject(with: saveduser) as! User
print(user2)
}
I would also suggest to read a little about it in here: NSCoding, Workaround for Swift Enum with raw type + case arguments?
Related
I'm using Codable for my WebRequest response which is returning some predefined string or number. So, I'm using Enums for those. But when some unexpected value arrive to Response at that my Codable fails to decode.
Here some code for better understanding.
class WebUser: Codable, Equatable {
static func == (lhs: WebUser, rhs: WebUser) -> Bool {
return lhs.id == rhs.id
}
...
var mobileNumberPrivacy: CommonPrivacyOption?
var emailPrivacy: CommonPrivacyOption?
var dobPrivacy: CommonPrivacyOption?
...
}
enum CommonPrivacyOption: Int, CaseIterable, Codable {
case privacyOnlyMe = 1, privacyPublic, privacyFriends, privacyFriendsOfFriends
//Does not help this optional init function
/*init?(rawValue: Int) {
switch rawValue {
case 1: self = .privacyOnlyMe
case 2: self = .privacyPublic
case 3: self = .privacyFriends
case 4: self = .privacyFriendsOfFriends
default: return nil
}
}*/
}
but sometimes from WebServer I'm getting, 0 for dobPrivacy at that time I'm getting DecodingError.dataCorrupted exception with context Cannot initialize CommonPrivacyOption from invalid Int value 0
As I expect to dobPrivacy nil when I get other values then 1/2/3/4.
EDIT:
let dict1 = [
"id": 2,
"mobileNumberPrivacy": 3,
"emailPrivacy": 4,
"dobPrivacy": 0 // Works perfectly with 1
]
do {
let data1 = try JSONSerialization.data(withJSONObject: dict1, options: .prettyPrinted)
let user1 = try JSONDecoder().decode(WebUser.self, from: data1)
print("User1 created")
}
catch DecodingError.dataCorrupted(let context) {
print(context.codingPath)
print(context.debugDescription)
}
catch {
print(error.localizedDescription)
}
I'm using this same Codable WebUser object for Profile detail, search users and many more.
so may be some times one more key will not present in WebRequest's response.
I recommend writing a property wrapper that handles this problem for you.
Specifically, let's write a property wrapper named NilOnDecodingError that turns any DecodingError into nil.
Here's the declaration of NilOnDecodingError:
#propertyWrapper
public struct NilOnDecodingError<Wrapped> {
public init(wrappedValue: Wrapped?) {
self.wrappedValue = wrappedValue
}
public var wrappedValue: Wrapped?
}
We've defined it to wrap any type, storing an Optional.
Now we can conform it to Decodable when the Wrapped type is Decodable:
extension NilOnDecodingError: Decodable where Wrapped: Decodable {
public init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
do {
wrappedValue = .some(try container.decode(Wrapped.self))
} catch is DecodingError {
wrappedValue = nil
}
}
}
We probably also want it to be Encodable when the Wrapped type is Encodable:
extension NilOnDecodingError: Encodable where Wrapped: Encodable {
public func encode(to encoder: Encoder) throws {
var container = encoder.singleValueContainer()
if let value = wrappedValue {
try container.encode(value)
} else {
try container.encodeNil()
}
}
}
Now we can wrap the appropriate fields of WebUser:
class WebUser: Codable {
let id: String
#NilOnDecodingError
var mobileNumberPrivacy: CommonPrivacyOption?
#NilOnDecodingError
var emailPrivacy: CommonPrivacyOption?
#NilOnDecodingError
var dobPrivacy: CommonPrivacyOption?
}
For testing, we'll want to print the fields of the decoded user:
extension WebUser: CustomStringConvertible {
var description: String {
return """
WebUser(
id: \(id),
mobileNumberPrivacy: \(mobileNumberPrivacy.map { "\($0)" } ?? "nil"),
emailPrivacy: \(emailPrivacy.map { "\($0)" } ?? "nil")),
dobPrivacy: \(dobPrivacy.map { "\($0)" } ?? "nil")))
"""
}
}
Now we can try it out:
let json = """
{
"id": "mrugesh",
"mobileNumberPrivacy": 1,
"emailPrivacy": 2,
"dobPrivacy": 1000
}
"""
let user = try! JSONDecoder().decode(WebUser.self, from: json.data(using: .utf8)!)
print(user)
Output:
WebUser(
id: mrugesh,
mobileNumberPrivacy: privacyOnlyMe,
emailPrivacy: privacyPublic),
dobPrivacy: nil))
You need to create a custom Decodable initializer inside WebUser:
class WebUser: Codable {
var dobPrivacy: CommonPrivacyOption?
// Rest of your properties here.
enum CodingKeys: String, CodingKey {
case dobPrivacy
// Add a case for each property you want to decode here.
}
required init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
// Use optional try to decode your enum so that when the
// decode fails because of wrong Int value, it will assign nil.
dobPrivacy = try? container.decode(CommonPrivacyOption.self, forKey: .dobPrivacy)
}
}
Alternatively, you can implement the Decodable initializer inside CommonPrivacyOption and add an additional case unknown like so:
enum CommonPrivacyOption: Int, Codable {
case privacyOnlyMe = 1
case privacyPublic, privacyFriends, privacyFriendsOfFriends
case unknown
init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
let value = try container.decode(Int.self)
// Try to initialize Self from value, if
// value is not 1, 2, 3, or 4, initialize Self to
// the unknown case.
self = .init(rawValue: value) ?? .unknown
}
}
It looks to me like the compiler selects the wrong init for the enum types, instead of init(rawValue) it uses init(from:) that is the one for decoding (which in a way makes sense)
Here is a solution where we override this behaviour by using a custom init(from) in WebUser that decodes the raw values and then creates an enum item
required init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
if let value = try container.decodeIfPresent(CommonPrivacyOption.RawValue.self, forKey: .mobileNumberPrivacy), let mobileNumberPrivacy = CommonPrivacyOption(rawValue: value) {
self.mobileNumberPrivacy = mobileNumberPrivacy
}
if let value = try container.decodeIfPresent(CommonPrivacyOption.RawValue.self, forKey: .emailPrivacy), let emailPrivacy = CommonPrivacyOption(rawValue: value) {
self.emailPrivacy = emailPrivacy
}
if let value = try container.decodeIfPresent(CommonPrivacyOption.RawValue.self, forKey: .dobPrivacy), let dobPrivacy = CommonPrivacyOption(rawValue: value) {
self.dobPrivacy = dobPrivacy
}
}
Below is a small example
extension WebUser: CustomStringConvertible {
var description: String {
"Mobile: \(mobileNumberPrivacy?.rawValue), email: \(emailPrivacy?.rawValue), dob: \(dobPrivacy?.rawValue)"
}
}
let data = """
{
"mobileNumberPrivacy": 1,
"dobPrivacy": 0
}
""".data(using: .utf8)!
do {
let result = try JSONDecoder().decode(WebUser.self, from: data)
print(result)
} catch {
print(error)
}
Mobile: Optional(1), email: nil, dob: nil
Of course if you can change your mind about 0 being translated to nil then I would suggest you extend the enum to support the 0 value
enum CommonPrivacyOption: Int, CaseIterable, Codable {
case none = 0
case privacyOnlyMe = 1, privacyPublic, privacyFriends, privacyFriendsOfFriends
}
Then it should work out of the box and you don't need to write any custom code.
I liked #alobaili's answer since it's simple and provided a good solution. One thing I wanted to improve is to make it more generic, so that any Decodable (and Codable) could do it with less code to write.
extension Decodable where Self: RawRepresentable, RawValue: Decodable {
static func initializedOptionalWith(decoder: Decoder, defaultValue: Self) throws -> Self {
let container = try decoder.singleValueContainer()
let value = try container.decode(RawValue.self)
return .init(rawValue: value) ?? defaultValue
}
}
Usage:
init(from decoder: Decoder) throws {
self = try .initializedOptionalWith(decoder: decoder, defaultValue: .unknown)
}
Thank you Rob Mayoff for a great answer. I would like to add just one thing - if the property is missing, decoding fails even if the property is optional. To prevent this failure, add the following extension:
extension KeyedDecodingContainer {
func decode<T>(_ type: NilOnDecodingError<T>.Type, forKey key: Self.Key) throws -> NilOnDecodingError<T> where T: Decodable {
try decodeIfPresent(type, forKey: key) ?? NilOnDecodingError<T>(wrappedValue: nil)
}
}
I am storing a transformable in CoreData and am trying to make it NSSecureCoding compliant, but I keep getting Unexpectedly found nil whilst unwrapping an optional value, when I try to decode
in the XCDataModel, I am set the property as transformable and use the custom class AssignedExerciseObjects, I have also set the transformer to AssignedExerciseObjectsValueTransformer
I have also registered the transformer in my CoreData stack
here is a copy of my transformer
#objc(AssignedExerciseObjectsValueTransformer)
final class AssignedExerciseObjectsValueTransformer: NSSecureUnarchiveFromDataTransformer {
static let name = NSValueTransformerName(rawValue: String(describing: AssignedExerciseObjectsValueTransformer.self))
override static var allowedTopLevelClasses: [AnyClass] {
return [NSArray.self, AssignedExerciseObjects.self]
}
/// Registers the transformer.
public static func register() {
let transformer = AssignedExerciseObjectsValueTransformer()
ValueTransformer.setValueTransformer(transformer, forName: name)
}
}
and here is a copy of my custom class
import Foundation
public class AssignedExerciseObjects: NSObject, NSSecureCoding {
public static var supportsSecureCoding: Bool = true
public var assignedExerciseObjects: [AssignedExerciseObject] = []
enum Key:String {
case assignedExerciseObjects = "assignedExerciseObjects"
}
init(assignedExerciseObjects: [AssignedExerciseObject]) {
self.assignedExerciseObjects = assignedExerciseObjects
}
public func encode(with aCoder: NSCoder) {
aCoder.encode(assignedExerciseObjects, forKey: Key.assignedExerciseObjects.rawValue)
}
public required convenience init?(coder aDecoder: NSCoder) {
let mAssignedExerciseObjects = aDecoder.decodeObject(of: NSArray.self, forKey: Key.assignedExerciseObjects.rawValue) as! [AssignedExerciseObject]
self.init(assignedExerciseObjects: mAssignedExerciseObjects)
}
}
public class AssignedExerciseObject: NSObject, NSSecureCoding {
public static var supportsSecureCoding: Bool = true
public var mainExercise: String = ""
public var prepareTime: String = ""
public var exerciseTime: String = ""
public var highIntensityTime: String = ""
public var restTime: String = ""
public var highLowSplit: Bool = false
public var isCompleted: Bool = false
public var isSkipped: Bool = false
public var order: Double = 0
enum Key:String {
case mainExercise = "mainExercise"
case prepareTime = "prepareTime"
case exerciseTime = "exerciseTime"
case highIntensityTime = "highIntensityTime"
case restTime = "restTime"
case highLowSplit = "highLowSplit"
case isCompleted = "isCompleted"
case isSkipped = "isSkipped"
case order = "order"
}
init(mainExercise: String, prepareTime: String, exerciseTime: String, highIntensityTime: String, restTime: String, highLowSplit: Bool, isCompleted: Bool, isSkipped: Bool, order: Double) {
self.mainExercise = mainExercise
self.prepareTime = prepareTime
self.exerciseTime = exerciseTime
self.highIntensityTime = highIntensityTime
self.restTime = restTime
self.highLowSplit = highLowSplit
self.isCompleted = isCompleted
self.isSkipped = isSkipped
self.order = order
}
public override init() {
super.init()
}
public func encode(with aCoder: NSCoder) {
aCoder.encode(mainExercise, forKey: Key.mainExercise.rawValue)
aCoder.encode(prepareTime, forKey: Key.prepareTime.rawValue)
aCoder.encode(exerciseTime, forKey: Key.exerciseTime.rawValue)
aCoder.encode(highIntensityTime, forKey: Key.highIntensityTime.rawValue)
aCoder.encode(restTime, forKey: Key.restTime.rawValue)
aCoder.encode(highLowSplit, forKey: Key.highLowSplit.rawValue)
aCoder.encode(isCompleted, forKey: Key.isCompleted.rawValue)
aCoder.encode(isSkipped, forKey: Key.isSkipped.rawValue)
aCoder.encode(order, forKey: Key.order.rawValue)
}
public required convenience init?(coder aDecoder: NSCoder) {
let mMainExercise = aDecoder.decodeObject(of: NSString.self, forKey: Key.mainExercise.rawValue)! as String
let mPrepareTime = aDecoder.decodeObject(of: NSString.self,forKey: Key.prepareTime.rawValue)! as String
let mExerciseTime = aDecoder.decodeObject(of: NSString.self,forKey: Key.exerciseTime.rawValue)! as String
let mHighIntensityTime = aDecoder.decodeObject(of: NSString.self,forKey: Key.highIntensityTime.rawValue)! as String
let mRestTime = aDecoder.decodeObject(of: NSString.self,forKey: Key.restTime.rawValue)! as String
let mHighLowSplit = aDecoder.decodeBool(forKey: Key.highLowSplit.rawValue)
let mIsCompleted = aDecoder.decodeBool(forKey: Key.isCompleted.rawValue)
let mIsSkipped = aDecoder.decodeBool(forKey: Key.isSkipped.rawValue)
let mOrder = aDecoder.decodeDouble(forKey: Key.order.rawValue)
self.init(mainExercise: String(mMainExercise), prepareTime:
String(mPrepareTime), exerciseTime: String(mExerciseTime), highIntensityTime: String(mHighIntensityTime), restTime: String(mRestTime), highLowSplit: Bool(mHighLowSplit), isCompleted: Bool(mIsCompleted), isSkipped: Bool(mIsSkipped), order: Double(mOrder))
}
}
this is where it gives me the error
let mAssignedExerciseObjects = aDecoder.decodeObject(of: NSArray.self, forKey: Key.assignedExerciseObjects.rawValue) as! [AssignedExerciseObject]
I'm still fairly new to swift, but I cannot for the life of me work out why it is returning a nil value?
when the record is created, it all seems to work fine, but when I try to display the record in a table, I get the error
any help is greatly appreciated
regards
Jamie
OK, I've fixed it
Adding my custom class fixed the issue
let mAssignedExerciseObjects = aDecoder.decodeObject(of: [AssignedExerciseObject.self, NSArray.self], forKey: Key.assignedExerciseObjects.rawValue) as! [AssignedExerciseObject]
I Have this enums:
#objc enum Groups: Int {
case large = 0
case medium = 1
case small = 2
case micro = 3
case all = 4
case favoriten = 5
}
enum Ordering:String {
case ascending
case descending
case nothing
}
enum QuantityStats:String {
case one
case two
case three
case four
case six
}
enum KeysState: String {
case listTableViewState
case listLabelState
}
These are part of this class:
class ListTableViewState: State, NSCoding {
private enum Keys: String {
case group
case statIntervalBase
case order
case quantityStats
}
var group: Groups {
didSet {
if initialized {
saveUserDefaults(withKey: KeysState.listTableViewState.rawValue, myType: self)
}
}
}
var statIntervalBase: StatIntervalBaseModel
var order: Ordering
var searchParameter: String?
var quantityStats: QuantityStats
init(group: Groups, statIntervalBase: StatIntervalBaseModel, order: Ordering, searchParameter: String?, quantityStats: QuantityStats) {
self.group = group
self.statIntervalBase = statIntervalBase
self.order = order
self.searchParameter = searchParameter
self.quantityStats = quantityStats
print("Group", Keys.group, "order", Keys.order)
super.init()
initialized = true
}
required convenience init?(coder aDecoder: NSCoder) {
// self.stage = Stage(rawValue: aDecoder.decodeObject(forKey: "stage") as String)
let group = Groups(rawValue: aDecoder.decodeObject(forKey: Keys.group.rawValue) as! Int)
let statIntervalBase = aDecoder.decodeObject(forKey: Keys.statIntervalBase.rawValue) as! StatIntervalBaseModel
let order = Ordering(rawValue: aDecoder.decodeObject(forKey: Keys.order.rawValue) as! String)
let quantityStats = QuantityStats(rawValue: aDecoder.decodeObject(forKey: Keys.quantityStats.rawValue) as! String)
self.init(group: group!, statIntervalBase: statIntervalBase, order: order!, searchParameter: "", quantityStats: quantityStats!)
}
func encode(with aCoder: NSCoder) {
aCoder.encode(group, forKey: Keys.group.rawValue)
aCoder.encode(statIntervalBase, forKey: Keys.statIntervalBase.rawValue)
aCoder.encode(order, forKey: Keys.order.rawValue)
aCoder.encode(quantityStats, forKey: Keys.quantityStats.rawValue)
}
}
This is the State Base class I made:
class State: NSObject {
var initialized = false
func saveUserDefaults<T>(withKey key: String, myType: T){
let archiver:Data = NSKeyedArchiver.archivedData(withRootObject: myType)
UserDefaults.standard.set(archiver, forKey: key)
UserDefaults.standard.synchronize()
}
func loadUserDefaults<T>(withKey key: String) -> T? {
var output: T?
let decoded = UserDefaults.standard.object(forKey: key)
if decoded != nil {
let data = decoded as! Data
output = (NSKeyedUnarchiver.unarchiveObject(with: data) as! T)
} else {
output = nil
}
return output
}
}
I want to save ListTableViewState with NSUserDefaults but I keep getting errors, I think I am saving the enums wrong. But I could not figure it out how to solve it.
This is the error I get:
2018-10-29 18:53:22.906915+0000 APP[40545:8188629] -[_SwiftValue
encodeWithCoder:]: unrecognized selector sent to instance
0x600001c7d7a0 2018-10-29 18:53:22.912060+0000 APP[40545:8188629]
* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[_SwiftValue
encodeWithCoder:]: unrecognized selector sent to instance
0x600001c7d7a0'
* First throw call stack:
You construct each enum value from rawValue in your init(coder:). Why don't you encode rawValue?
required convenience init?(coder aDecoder: NSCoder) {
let group = Groups(rawValue: aDecoder.decodeInteger(forKey: Keys.group.rawValue))
let statIntervalBase = aDecoder.decodeObject(forKey: Keys.statIntervalBase.rawValue) as! StatIntervalBaseModel
let order = Ordering(rawValue: aDecoder.decodeObject(forKey: Keys.order.rawValue) as! String)
let quantityStats = QuantityStats(rawValue: aDecoder.decodeObject(forKey: Keys.quantityStats.rawValue) as! String)
self.init(group: group!, statIntervalBase: statIntervalBase, order: order!, searchParameter: "", quantityStats: quantityStats!)
}
func encode(with aCoder: NSCoder) {
aCoder.encode(group.rawValue, forKey: Keys.group.rawValue)
aCoder.encode(statIntervalBase, forKey: Keys.statIntervalBase.rawValue)
aCoder.encode(order.rawValue, forKey: Keys.order.rawValue)
aCoder.encode(quantityStats.rawValue, forKey: Keys.quantityStats.rawValue)
}
And I think you can write loadUserDefaults(withKey:) as:
func loadUserDefaults<T>(withKey key: String) -> T? {
var output: T? = nil
if let data = UserDefaults.standard.data(forKey: key) {
output = (NSKeyedUnarchiver.unarchiveObject(with: data) as! T)
}
return output
}
(Shouldn't this be static?)
I feel like I'm going way too far for what should really be a simple thing. With the below code, the error that I get is: Cannot invoke 'decode' with an argument list of type '(GameOfLife.Cell, forKey: GameOfLife.Cell.CodingKeys)'
extension GameOfLife {
enum Cell: Equatable, Codable {
case alive
case born
case dying
case dead
var isAlive: Bool {
switch self {
case .alive, .born: return true
case .dying, .dead: return false
}
}
var isDead: Bool {
switch self {
case .alive, .born: return false
case .dying, .dead: return true
}
}
func equalTo(_ rhs: Cell) -> Bool {
switch (self) {
case .alive, .born:
return rhs.isAlive
case .dead, .dying:
return rhs.isDead
}
}
init(_ living: Bool) {
self = living ? .alive : .dead
}
enum CodingKeys: CodingKey {
case alive
case born
case dying
case dead
}
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
do {
let leftValue = try container.decode(GameOfLife.Cell.alive, forKey: CodingKeys.alive)
self = GameOfLife.Cell.alive
} catch {
let leftValue = try container.decode(GameOfLife.Cell.born, forKey: CodingKeys.born)
self = GameOfLife.Cell.born
} catch {
let leftValue = try container.decode(GameOfLife.Cell.dying, forKey: CodingKeys.dying)
self = GameOfLife.Cell.dying
} catch {
let leftValue = try container.decode(GameOfLife.Cell.dead, forKey: CodingKeys.dead)
self = GameOfLife.Cell.dead
}
}
func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
switch self {
case .alive:
try container.encode("alive", forKey: .alive)
case .born:
try container.encode("born", forKey: .born)
case .dying:
try container.encode("dying", forKey: .dying)
case .dead:
try container.encode("dead", forKey: .dead)
}
}
}
}
The first parameter of decode( is the expected type of the decoded object. As you are encoding the enum cases a String, it's supposed to be String
let leftValue = try container.decode(String.self, forKey: .alive) // The compiler can infer the type `CodingKeys`
However the entire init(from method makes no sense. If an error occurs inside a catch expression it is not going to be caught in the subsequent catch expression.
To encode an enum case as String just declare the raw type of the enum as String and delete the CodingKeys and the init(from and encode(to methods. And if you want to adopt Equatable you have to implement ==
extension GameOfLife : Codable {
enum Cell: String, Equatable, Codable {
case alive, born, dying, dead
var isAlive: Bool {
switch self {
case .alive, .born: return true
case .dying, .dead: return false
}
}
// isDead can be simplified
var isDead: Bool {
return !isAlive
}
static func == (lhs: Cell, rhs: Cell) -> Bool {
switch (lhs.isAlive, rhs.isAlive) {
case (true, true), (false, false): return true
default: return false
}
}
init(_ living: Bool) {
self = living ? .alive : .dead
}
}
}
I'm trying to encode an object and i have some troubles. It work's fine with strings, booleans and else, but i don't know how to use it for enum.
I need to encode this:
enum Creature: Equatable {
enum UnicornColor {
case yellow, pink, white
}
case unicorn(UnicornColor)
case crusty
case shark
case dragon
I'm using this code for encode:
func saveFavCreature(creature: Dream.Creature) {
let filename = NSHomeDirectory().appending("/Documents/favCreature.bin")
NSKeyedArchiver.archiveRootObject(creature, toFile: filename)
}
func loadFavCreature() -> Dream.Creature {
let filename = NSHomeDirectory().appending("/Documents/favCreature.bin")
let unarchived = NSKeyedUnarchiver.unarchiveObject(withFile: filename)
return unarchived! as! Dream.Creature
}
Here is required functions (model.favoriteCreature == Dream.Creature)
override func encode(with aCoder: NSCoder) {
aCoder.encode(model.favoriteCreature, forKey: "FavoriteCreature")
aCoder.encode(String(), forKey: "CreatureName")
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
let favoriteCreature = aDecoder.decodeObject(forKey: "FavoriteCreature")
let name = aDecoder.decodeObject(forKey: "CreatureName")
}
It works fine with "name", i think the problem is in aCoder.encode() coz i don't know what type to write there. I get next error when run:
-[_SwiftValue encodeWithCoder:]: unrecognized selector sent to instance
-[NSKeyedArchiver dealloc]: warning: NSKeyedArchiver deallocated without having had -finishEncoding called on it.
I read some advices in comments and can assume that i have no rawValues in enum Creature, i made raw type "String" in that enum:
enum Creature: String, Equatable {
enum UnicornColor {
case yellow, pink, white
}
case unicorn(UnicornColor)
case crusty
case shark
case dragon
Now i have this error:
Enum with raw type cannot have cases with arguments.
Also i read that associated values and raw values can't coexist. Maybe there is some other way to archive enum without raw values?
Hope someone can help me, thank's
You are dealing with a problem that arises because Swift native features don't always play well with Objective-C. NSCoding has its roots in the Objective-C world, and Objective-C doesn't know anything about Swift Enums, so you can't simply archive an Enum.
Normally, you could just encode/decode the enumeration using raw values, but as you found, you can't combine associated types and raw values in a Swift enumeration.
Unfortunately this means that you will need to build your own 'raw' value methods and handle the cases explicitly in the Creature enumeration:
enum Creature {
enum UnicornColor: Int {
case yellow = 0, pink, white
}
case unicorn(UnicornColor)
case crusty
case shark
case dragon
init?(_ creatureType: Int, color: Int? = nil) {
switch creatureType {
case 0:
guard let rawColor = color,
let unicornColor = Creature.UnicornColor(rawValue:rawColor) else {
return nil
}
self = .unicorn(unicornColor)
case 1:
self = .crusty
case 2:
self = .shark
case 3:
self = .dragon
default:
return nil
}
}
func toRawValues() -> (creatureType:Int, unicornColor:Int?) {
switch self {
case .unicorn(let color):
let rawColor = color.rawValue
return(0,rawColor)
case .crusty:
return (1,nil)
case .shark:
return (2,nil)
case .dragon:
return (3,nil)
}
}
}
You can then encode/decode like this:
class SomeClass: NSObject, NSCoding {
var creature: Creature
init(_ creature: Creature) {
self.creature = creature
}
required init?(coder aDecoder: NSCoder) {
let creatureType = aDecoder.decodeInteger(forKey: "creatureType")
let unicornColor = aDecoder.decodeInteger(forKey: "unicornColor")
guard let creature = Creature(creatureType, color: unicornColor) else {
return nil
}
self.creature = creature
super.init()
}
func encode(with aCoder: NSCoder) {
let creatureValues = self.creature.toRawValues()
aCoder.encode(creatureValues.creatureType, forKey: "creatureType")
if let unicornColor = creatureValues.unicornColor {
aCoder.encode(unicornColor, forKey: "unicornColor")
}
}
}
Testing gives:
let a = SomeClass(.unicorn(.pink))
var data = NSMutableData()
let coder = NSKeyedArchiver(forWritingWith: data)
a.encode(with: coder)
coder.finishEncoding()
let decoder = NSKeyedUnarchiver(forReadingWith: data as Data)
if let b = SomeClass(coder: decoder) {
print(b.creature)
}
unicorn(Creature.UnicornColor.pink)
Personally, I would make Creature a class and use inheritance to deal with the variation between unicorns and other types
The main problem for your issue is that you cannot pass Swift enums to encode(_:forKey:).
This article shown by Paulw11 will help you solve this part. If the enum can easily have rawValue, it's not too difficult.
But, as you see, Enum with raw type cannot have cases with arguments.
Simple enums can easily have rawValue like this:
enum UnicornColor: Int {
case yellow, pink, white
}
But enums with associate values, cannot have rawValue in this way. You may need to manage by yourself.
For example, with having inner enum's rawValue as Int :
enum Creature: Equatable {
enum UnicornColor: Int {
case yellow, pink, white
}
case unicorn(UnicornColor)
case crusty
case shark
case dragon
static func == (lhs: Creature, rhs: Creature) -> Bool {
//...
}
}
You can write an extension for Dream.Creature as:
extension Dream.Creature: RawRepresentable {
var rawValue: Int {
switch self {
case .unicorn(let color):
return 0x0001_0000 + color.rawValue
case .crusty:
return 0x0002_0000
case .shark:
return 0x0003_0000
case .dragon:
return 0x0004_0000
}
}
init?(rawValue: Int) {
switch rawValue {
case 0x0001_0000...0x0001_FFFF:
if let color = UnicornColor(rawValue: rawValue & 0xFFFF) {
self = .unicorn(color)
} else {
return nil
}
case 0x0002_0000:
self = .crusty
case 0x0003_0000:
self = .shark
case 0x0004_0000:
self = .dragon
default:
return nil
}
}
}
(In fact, it is not an actual rawValue and you'd better rename it for a more appropriate name.)
With a definition like shown above, you can utilize the code shown in the link above.
To simplify the coding/decoding you could provide an initializer for Creature requiring a Data and a computed property named data. As Creature changes or as new associated values are added, the interface to NSCoding does not change.
class Foo: NSObject, NSCoding {
let creature: Creature
init(with creature: Creature = Creature.crusty) {
self.creature = creature
super.init()
}
required init?(coder aDecoder: NSCoder) {
guard let data = aDecoder.decodeObject(forKey: "creature") as? Data else { return nil }
guard let _creature = Creature(with: data) else { return nil }
self.creature = _creature
super.init()
}
func encode(with aCoder: NSCoder) {
aCoder.encode(creature.data, forKey: "creature")
}
}
A serialization of Creature into and out of Data could be accomplished like this.
enum Creature {
enum UnicornColor {
case yellow, pink, white
}
case unicorn(UnicornColor)
case crusty
case shark
case dragon
enum Index {
static fileprivate let ofEnum = 0 // data[0] holds enum value
static fileprivate let ofUnicornColor = 1 // data[1] holds unicorn color
}
init?(with data: Data) {
switch data[Index.ofEnum] {
case 1:
switch data[Index.ofUnicornColor] {
case 1: self = .unicorn(.yellow)
case 2: self = .unicorn(.pink)
case 3: self = .unicorn(.white)
default:
return nil
}
case 2: self = .crusty
case 3: self = .shark
case 4: self = .dragon
default:
return nil
}
}
var data: Data {
var data = Data(count: 2)
// the initializer above zero fills data, therefore serialize values starting at 1
switch self {
case .unicorn(let color):
data[Index.ofEnum] = 1
switch color {
case .yellow: data[Index.ofUnicornColor] = 1
case .pink: data[Index.ofUnicornColor] = 2
case .white: data[Index.ofUnicornColor] = 3
}
case .crusty: data[Index.ofEnum] = 2
case .shark: data[Index.ofEnum] = 3
case .dragon: data[Index.ofEnum] = 4
}
return data
}
}