Unrecognized selector sent to instance while archi

2019-01-18 03:48发布

问题:

-(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.

回答1:

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.



回答2:

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
  }
}


回答3:

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.



回答4:

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



回答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