Unrecognized selector sent to instance while archiving data (NSCoding)

前端 未结 5 1895
名媛妹妹
名媛妹妹 2020-12-09 09:48
-(void)transformObjects:(NSMutableArray*)array key:(NSString*)key
{
    NSMutableArray* archiveArray = [[NSMutableArray alloc]initWithCapacity:array.count];

    for         


        
相关标签:
5条回答
  • 2020-12-09 10:19

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

    0 讨论(0)
  • 2020-12-09 10:21

    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.

    0 讨论(0)
  • 2020-12-09 10:25

    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

    0 讨论(0)
  • 2020-12-09 10:34

    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
      }
    }
    
    0 讨论(0)
  • 2020-12-09 10:36

    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.

    0 讨论(0)
提交回复
热议问题