0

I want to store custom objects in NSUserDefaults. I read the Apple doc and know NSUserDefaults can store only property lists such as

NSArray, NSNumber, NSData, NSDate, NSDictionary and NSString.

If I want to store custom object, then I need to implement NSCoding protocol.

I need someone can clarify a little bit for the following class:

@interface MyDrawBlock : BlockInfo
{
    CGPoint _blockUpLeft;
    CAShapeLayer* _shapeLayer;
    int _blockColorFlag;
    int _strokeColorFlag;
}

I have CAShapeLayer* _shaperLayer in my class. How can I store MyDrawBlock in NSUserDefaults?

If I implement NSCoding protocol, then how I can store CAShapeLayer* _shapeLayer.

1234
  • 539
  • 3
  • 12
  • You can;t store a file in NSUserDefualts. NSUserDefaults is for storing simple keys/arrays. – Teja Nandamuri Jan 11 '16 at 21:22
  • this is not duplicated. My object is different from the given answer. Please read my question carefully. – 1234 Jan 11 '16 at 21:33
  • This looks like an [X/Y problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). Step back and tell us what you are trying to do, and why you think you need to do it that way. In general, if you're trying to encode a view object (And a CAShapeLayer is a view object for the purpose of this discussion) then you're doing something wrong. View objects are for showing content, not for saving state. There is probably a better/more appropriate way to accomplish what you are trying to accomplish. – Duncan C Jan 11 '16 at 22:02

1 Answers1

0

Try using NSKeyedArchiver and NSKeyedUnarchiver.

Here look at this answer for a similar question: https://stackoverflow.com/a/2315972/4160199

Community
  • 1
  • 1
quant24
  • 393
  • 6
  • 22