Unrecognized selector sent to instance while archiving data (NSCoding)
Asked Answered
F

5

22
-(void)transformObjects:(NSMutableArray*)array key:(NSString*)key
{
    NSMutableArray* archiveArray = [[NSMutableArray alloc]initWithCapacity:array.count];

    for (Furniture *furniture in array) {

        // The error occurs on the line below
        NSData *furnitureEncodedObject = [NSKeyedArchiver archivedDataWithRootObject:furniture];
        [archiveArray addObject:furnitureEncodedObject];
    }

    NSUserDefaults *userData = [NSUserDefaults standardUserDefaults];
    [userData setObject:archiveArray forKey:key];
}

Error log:

2014-03-04 10:55:27.881 AppName[10641:60b] -[Furniture encodeWithCoder:]: unrecognized selector sent to instance 0x15d43350

I have no idea why do I get "unrecognized selector sent to instance" when trying to archive an object.

Ferrocyanide answered 4/3, 2014 at 10:6 Comment(6)
What is the selector?Espadrille
did you check furnitureEncodedObject in consoleDacosta
[NSKeyedArchiver archivedDataWithRootObject:furniture]; here furniture should be furniture.someobject... try onceWenoa
Is Furniture derived from NSObject? If yes, refer to: #9929339Basis
Are you sure that your Furniture class conforms to the NSCoding protocol?Forrester
You are all right..I haven't implemented NSCoding protocol. Totally forgot about itFerrocyanide
M
42

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.

Mohler answered 4/3, 2014 at 10:14 Comment(0)
C
9

You'll need to implement NSCoding - here is an example with an object called SNStock that has two string properties, ticker and name:

import Foundation

class SNStock: NSObject, NSCoding
{
  let ticker: NSString
  let name: NSString

  init(ticker: NSString, name: NSString)
  {
    self.ticker = ticker
    self.name = name
  }

  //MARK: NSCoding

  required init(coder aDecoder: NSCoder) {
    self.ticker = aDecoder.decodeObjectForKey("ticker") as! NSString
    self.name = aDecoder.decodeObjectForKey("name") as! NSString
  }

  func encodeWithCoder(aCoder: NSCoder) {
    aCoder.encodeObject(ticker, forKey: "ticker")
    aCoder.encodeObject(name, forKey: "name")
  }

  //MARK: NSObjectProtocol

  override func isEqual(object: AnyObject?) -> Bool {
    if let object = object as? SNStock {
      return self.ticker == object.ticker &&
        self.name == object.name
    } else {
      return false
    }
  }

  override var hash: Int {
    return ticker.hashValue
  }
}
Crittenden answered 16/4, 2015 at 18:29 Comment(0)
P
5

For Swift 4.1 (tested code)

import UIKit
import SwiftyJSON

class UserObject: NSObject, NSCoding {
    var username: String? = ""
    var userID: String? = ""
    var user_email: String? = ""
    var name: String? = ""
    var age: String? = ""
    var gender: String? = ""

    override init() {
        super.init()
    }

    init(dictionary: JSON) {
        //User data initialize...
        username = dictionary["username"].stringValue
        userID = dictionary["iUserID"].stringValue
        user_email = dictionary["email"].stringValue
        name = dictionary["name"].stringValue
        age = dictionary["age"].stringValue
        gender = dictionary["gender"].stringValue
    }

    required public init(coder aDecoder: NSCoder) {
        username = aDecoder.decodeObject(forKey: "username") as? String
        userID = aDecoder.decodeObject(forKey: "iUserID") as? String
        user_email = aDecoder.decodeObject(forKey: "email") as? String
        name = aDecoder.decodeObject(forKey: "name") as? String
        age = aDecoder.decodeObject(forKey: "age") as? String
        gender = aDecoder.decodeObject(forKey: "gender") as? String
    }

    func encode(with aCoder: NSCoder) {
        aCoder.encode(username, forKey: "username")
        aCoder.encode(userID, forKey: "iUserID")
        aCoder.encode(user_email, forKey: "email")
        aCoder.encode(name, forKey: "name")
        aCoder.encode(age, forKey: "age")
        aCoder.encode(gender, forKey: "gender")
    }
}

Note : NSCoding protocol is important

Precipitous answered 13/9, 2018 at 11:8 Comment(0)
A
4

You have a custom class Furniture which you are trying to archive with NSKeyedArchiver. In order for this to work, the Furniture class needs to conform to the < NSCoding > protocol. Which means implementing the encodeWithCoder: and initWithCoder: methods.

Currently you don't implement these methods. You need to add them.

Acaulescent answered 4/3, 2014 at 10:14 Comment(0)
B
1

I think your Furniture class does not implement the NSCoding protocol.

Belgrade answered 4/3, 2014 at 10:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.