1
votes

I have these lines in my code :

[defaults setValue:someValue forKey:@"someValue"];
[animationGroup setValue:@"someValue" forKey:@"someValue"];  // CAAnimationGroup
NSString* animName = [theAnimation valueForKey:@"someValue"]; // CAAnimation

But reading the Apple Reference Documentation :

I don't see any reference to these methods, even parsing hierarchy or conformed protocols. From where do they come ? Is their use allowed ?

2

2 Answers

4
votes

These are Key-Value Coding methods; if those classes are Key-Value Coding compliant for those keys, then you can use them without issue. With respect to Core Animation, both CALayer and CAAnimation are KVC compliant, as detailed in this programming guide. For more general information, refer to the NSKeyValueCoding informal protocol and the Key-Value Coding Programming Guide.

2
votes

As a side note that confused me as well. One of the links above provides:

Both CALayer and CAAnimation are key-value coding compliant container classes, allowing you to set values for arbitrary keys. That is, while the key “someKey” is not a declared property of the CALayer class, however you can still set a value for the key “someKey”

meaning that you can safely send KVC-like setValue:forKey: and valueForKey: messages and not have to worry about the fact that @"someKey" is not a real KVC-compliant property (hence "arbitrary keys"). If you try this on other objects, exceptions may be thrown. I admit I'm not sure how to find out easily if other classes have this behavior, it certainly would be useful to have this behavior for free in many other places.