存档数据时发送到实例的无法识别的选择器(NSCoding)

时间:2014-03-04 10:06:09

标签: ios nsdata unrecognized-selector

-(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];
}

错误日志:

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

我不知道为什么我会得到"无法识别的选择器发送到实例"在尝试存档对象时。

5 个答案:

答案 0 :(得分:38)

您需要在Furniture对象中实现NSCoding协议:

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

基本上,您指定应该写入(编码)的内容和从文件中读取的内容(已解码)。通常,对于要存储在文件中的每个属性,您可以像我在示例中所做的那样。

答案 1 :(得分:7)

您需要实现NSCoding - 这是一个名为SNStock的对象的示例,它有两个字符串属性,tickername

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

答案 2 :(得分:3)

您有一个自定义类Furniture,您尝试使用NSKeyedArchiver进行存档。为了使其工作,Furniture类需要符合< NSCoding >协议。这意味着实施encodeWithCoder:initWithCoder:方法。

目前您尚未实施这些方法。你需要添加它们。

答案 3 :(得分:3)

对于Swift 4.1(经过测试的代码)

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

注意:NSCoding协议很重要

答案 4 :(得分:1)

我认为您的Furniture类没有实现NSCoding协议。