Search code examples
iphoneobjective-ccore-animation

How can I store data in a CALayer?


I am creating a grid of CALayers, is there a way I can set a tag or something so that I can identify it if I need to?

I can probably just make a data model with an NSArray to keep track, but it would be easier for my implementation if I could just store it in the layer.


Solution

  • CALayer is a KVC compliant class, so you can set a value for any key.

    For example:

    [myLayer setValue: @"A TAG" forKey: @"someKey"];
    

    You can then retrieve the value using:

    value = [myLayer valueForKey: @"someKey"];
    

    See the Apple docs for more details.