-2
class AddBodyMeasurments
{
    static let sharedInstance = AddBodyMeasurments()
    var BodyMeasurements:AddMeasurment!

    private init()
    {
        BodyMeasurements = AddMeasurment.init(json: [:])
    }
    func SaveValue(value:AddMeasurment)
    {
        BodyMeasurements = value
    }
}

I have tried the following :

let data = NSKeyedArchiver.archivedDataWithRootObject(obj)
NSUserDefaults.standardUserDefaults().setObject(data, forKey: "folks")

I am getting Following error :

2017-08-17 12:37:46.009 Fiyre[15749:1061631] *** NSForwarding: warning: object 0x7fca71d18700 of class 'Fiyre.AddBodyMeasurments' does not implement methodSignatureForSelector: -- trouble ahead Unrecognized selector -[Fiyre.AddBodyMeasurments replacementObjectForKeyedArchiver:]

Thanks Advance.

Ivan
  • 34,531
  • 8
  • 55
  • 100
Arjun Patel
  • 1,394
  • 14
  • 23

2 Answers2

1

The error message reveals that the class does not inherit from NSObject.

  • Inheritance from NSObject is required to conform to NSCoding.
  • NSCoding is required to archive custom classes.
vadian
  • 274,689
  • 30
  • 353
  • 361
0

Actually, you're not saving a singleton object but an object in general. After you deserialize you object (get its instance from NSUserDefaults) you will store that instance to your singleton object or more precisely you will instantiate your singleton object with deserialized object.

class AddBodyMeasurments
{
    static let sharedInstance = AddBodyMeasurments()
    var bodyMeasurements:AddMeasurment!

    private init()
    {
        // deserializing object of AddBodyMeasurments type from NSDefaults.
        let deserialized = getFromUserDefaults()

        self.bodyMeasurements = deserialized.bodyMeasurements
    }
}
Ivan
  • 264
  • 3
  • 14