Unrecognized Selector Sent to Instance While Archiving Data (Nscoding)

Unrecognized selector sent to instance while archiving data (NSCoding)

You need to implement NSCoding protocol inside your Furniture object:

- (void)encodeWithCoder:(NSCoder *)aCoder{
[aCoder encodeObject:self.yourpoperty forKey:@"PROPERTY_KEY"];
}

-(id)initWithCoder:(NSCoder *)aDecoder{
if(self = [super init]){
self.yourpoperty = [aDecoder decodeObjectForKey:@"PROPERTY_KEY"];
}
return self;
}

Basically you specify what should be written (encoded) and read from a file (decoded). Usually for each property you want to store in a file, you make same as I did here in an example.

[__SwiftValue encodeWithCoder:]: unrecognized selector sent to instance

NSKeyedArchiver is for Cocoa Objective C objects only. Only NSCoding adopters can be archived with an NSKeyedArchiver. You would need an NSDictionary containing only NSCoding types. Well, you don’t have one.

Can't get NSKeyedArchiver to work: unrecognized selector

There is a very simple article on NSHipster about how to make your custom classes NSCoding/NSKeyedArchiver compliant. I recommend giving that a look.

As a basic answer, in order to make your object work with NSKeyedArchiver you need to tell the coder/decoder how to encode/decode your object. For example, if this is your class:

class Participant {

let name: String
let id: String
let age: Int

init(name: String, id: String, age: Int) {
self.name = name
self.id = id
self.age = age
}

}

You would need to make the following modifications to make it work with NSKeyedArchiver. First, declare that you are conforming to NSObject and NSCoding protocols. Then implement the encode methods and the convenience init?(coder:_) initializer:

class Participant: NSObject, NSCoding {

let name: String
let id: String
let age: Int

init(name: String, id: String, age: Int) {
self.name = name
self.id = id
self.age = age
}

required convenience init?(coder aDecoder: NSCoder) {
guard let name = aDecoder.decodeObject(forKey: "name") as? String,
let id = aDecoder.decodeObject(forKey: "id") as? String,
let age = aDecoder.decodeObject(forKey: "age") as? Int else { return nil }

self.init(name: name, id: id, age: age)
}

func encode(with aCoder: NSCoder) {
aCoder.encode(self.name, forKey: "name")
aCoder.encode(self.id, forKey: "id")
aCoder.encode(self.age, forKey: "age")
}

}

NSKeyedArchiver - Unrecognized selector sent to instance error

Along with inputs from JPetric and Valentin Radu I have modified the code by a little and the error is removed.

Below is my solution:

class UserDetails : NSObject,NSCoding {

//var token:String?
var token:String!

//var agentId: Int?
var agentId: Int!

//var userId:Int?
var userId:Int!

//to directly get values from JSON output

init?(user: [String: Any]) {

guard let token = user["token"] as? String,
let agentId = user["agent_id"] as? Int,
let userId = user["user_id"] as? Int
else{
return nil
}

self.token = token;
self.agentId = agentId;
self.userId = userId;
}

init(pToken:String,pAgentId:Int,pUserId:Int)
{
token = pToken;
agentId = pAgentId;
userId = pUserId;
}

//TO SAVE IN NSUSERDEFAULTS

required init?(coder aDecoder: NSCoder){
self.token = aDecoder.decodeObject(forKey: "token") as? String
self.agentId = aDecoder.decodeObject(forKey: "agentId") as? Int
self.userId = aDecoder.decodeObject(forKey: "userId") as? Int

return self
}

public func encode(with aCoder: NSCoder) {
aCoder.encode(token,forKey:"token")
aCoder.encode(agentId,forKey:"agentId")
}

}

Swift 3 Xcode 8 - SwiftValue encodeWithCoder - unrecognized selector sent to instance

The problem is that you are trying to archive an optional. Replace this line:

if (object != nil) {

with:

if let object = object {

Core Data error: -[myclass encodeWithCoder:]: unrecognized selector sent to instance

How did you define the myClass ivar in your entity? You can use a Transformable attribute to hold a non standard type. The idea is that Core Data uses behind the scenes an instance of NSValueTransformer to convert the attribute to and from an instance of NSData.

Core Data then stores the data instance to the persistent store.

By default is used the NSKeyedUnarchiverFromDataTransformerName, this means your custom class must implement the NSCoding protocol (with support for keyed-archiving)

Check "Non-Standard Persistent Attributes" in the Core Data Programming Guide for more details and description of another way to accomplish the same.



Related Topics



Leave a reply



Submit