Take the 2-minute tour ×

I have a simple iphone application that paints blocks using a subclass of CALayer and am trying to find the best way to save state or persist the currently created layers.

I tried using Brad Larson's answer from this previous question on storing custom objects in the NSUserDefaults, which worked for persisting my subclass of CALayer, but not it's basic state like geometry and background color, so they were there but did not render on relaunch.

I made my declared instance variables conform to the NSCoding protocol but do not know how to make CALayer's properties do the same without re-declaring all of it's properties. Or is this not the correct/best approach altogether?

Here is the code I'm using to archive the array of layers:

[[NSUserDefaults standardUserDefaults] setObject:[NSKeyedArchiver archivedDataWithRootObject:viewController.view.layer.sublayers] forKey:@"savedArray"];

And here is the code I'm using to reload my layers in -viewDidLoad:

    NSUserDefaults *currentDefaults = [NSUserDefaults standardUserDefaults];

NSData *dataRepresentingSavedArray = [currentDefaults objectForKey:@"savedArray"];

if (dataRepresentingSavedArray != nil) {

	[self restoreStateWithData:dataRepresentingSavedArray];

And in -restoreStateWithData:

    NSArray *savedLayers = [NSKeyedUnarchiver unarchiveObjectWithData:data];

if (savedLayers != nil) {

	for(layer in savedLayers) {

		[self.view.layer addSublayer:layer];


	[spaceView.layer layoutSublayers];		
share|improve this question

2 Answers 2

up vote 2 down vote accepted

Cocoa (and Cocoa Touch) are mostly based on a model-view-controller organization. CALayers are in the view tier. This leads to two questions:

  1. What part of your model does your layer present to the user?
  2. How do you make that part of the model persist?

The answers to those questions are your solution.

Nowadays, for a new app, the simplest (certainly most extensible) path to a persistent model is probably Core Data.

share|improve this answer
Peter's point here is key. Focus on the data you want to store, not the CALayer implementation. The CALayer should display your data, not be your data. When you've created a data class, storing that should be fairly straightforward, and when you load it again, you can easily regenerate a new CALayer to display it. –  Rob Napier Jul 22 '09 at 3:59
You start out right, but then you appear to start stuffing presentation details in your model. I'm not sure what your app is; if it's a graphics app, then maybe “frame” is a legitimate part of the model. Otherwise, I doubt it. If frame rectangles and corner radii and colors are not part of the data that the user is using your application to curate and edit, then they do not belong in the model, and you should leave them to your subclass of CALayer to handle. Your subclass of CALayer should also have one other property, by which you give the layer the model object that it should present. –  Peter Hosey Jul 23 '09 at 23:40
The controller owns and creates both the model objects and the view objects (layers), and gives each model object to the view (layer) that will display it. Controller and view (layer) share the same model object, and each retain it. –  Peter Hosey Jul 24 '09 at 19:01
“I pretty much got it working except UIColor will not encode, archive, then un-archive successfully. Is there a way to archive structures, so that I could just persist the CGColorRef without converting it to a UIColor?” CGColor isn't a structure. UIColor's documentation says that it conforms to NSCoding, so whatever archiver and unarchiver you're using should be able to encode it. If that isn't working, you should file a bug in RadarWeb. –  Peter Hosey Jul 24 '09 at 19:03
Ok thanks. I'll file a bug on UIColor. But what is a CGColorRef if it's not a struct? and is there a way to save it? –  Joe Ricioppo Jul 24 '09 at 20:59

Just to be precise, according to the Apple docs, the CALayer is actually the model and not the view in the MVC pattern.


The view behind the layer is actually the view part of the pattern. A layer cannot be displayed without a backing view.

It seems to me that it should be a perfectly legitimate candidate for data serialization. Take a look at the KVC Extensions for CALayer. Particularly look at:

- (BOOL)shouldArchiveValueForKey:(NSString *)key
share|improve this answer
This comment is outdated :-). The latest iPhone SDK also supports archiving CALayers. –  jarjar Sep 13 '10 at 0:06
yep but it doesn't work from personal experience –  Max MacLeod Nov 11 '11 at 12:27
CALayer is a model class for layer trees, that is right, but that does not in any way make it part of the MVC-pattern model. The model in MVC is only your applications data and logic. –  Ahti Feb 19 '13 at 2:33

Your Answer


