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
Malloc Error "Can't Allocate Region" Failed with Error Code 12. Any Idea How to Resolve This
Passing Data Between 2 Uiviewcontroller Using Delegate and Protocol
Swift - Pushviewcontroller from Appdelegate, Rootviewcontroller.Navigationcontroller Is Nil
Using Generic In-App Purchase Items for a Dynamic Range of Digital Products
Attribute Unavailable Warning on iOS Versions Prior to 8.0
Uitableviewcell Height Resize When Image Is Downloaded
What's Currently the "Correct" Way to Set a Uiview's Corner Radius
Afnetworking - Do Not Cache Response
Xcode 6.3.2 Can't Run on Osx 10.11 El Capitan Developer Preview 3
How Big Can the Payload Be When Sending Data via Watchconnectivity
Detect and Use Optional External C Library at Runtime in Objective-C
Creating a Tutorial for My iOS App with Page Control
Automating Xcode' Debug > Simulate Location Command
Marker Click Event on React Native Maps Not Working in React iOS
Uitableview Scrolls to Top When Reloading Cells with Changing Cell Heights
How to Reset the Application Data After Each Test with Xcode 7 UI Testing