I have never carefully read the usage of KVC. I assume that any key-value pair can be stored in the NSObject object. I encountered a problem when using the result.
A simple displacement Animation:
CAKeyframeAnimation *keyPosi=[CAKeyframeAnimation animationWithKeyPath:==];
I want to remove the UILabel from the screen after the animation ends, and then add the callback after the animation ends:
- ()animationDidStop:(CAAnimation *
However, it seems that the UILabel object cannot be obtained from the callback method parameters. (You may have provided APIs to obtain the UIlabel object. I hope you can kindly advise them ), if it is not necessary to set a global variable pointer to get the UILabel object here, I think of KVC, so I added the following sentence when creating an animation:
[keyPosi setValue:label forKey:];
In this way, you can obtain the UILabel object through the anim parameter in the callback method of the animation end:
-(Void) animationDidStop :( CAAnimation *) anim finished :( BOOL) flag {
If (flag ){
UIView * vie = [anim valueForKey: @ "xx"];
[Vie removeFromSuperview];
}
}
The effect has been achieved. However, is KVC used in this way? Let's look at another example:
NSObject *obj = forKey:];
Running error:
*** Terminating app due to uncaught exception , reason:
In the past, the key (Path) parameter of setValue: forKey and setValue: forKeyPath was not written at will. It must be the member variable name (or instance method) defined in the class ), this article is very clear: http://www.cnblogs.com/jay-dong/archive/2012/12/13/2815778.html. After that, why can the setValue: forKey key used by CAKeyframeAnimation be successfully set? Class does not have the member variable xx. The method setValue: forUndefinedKey :,
setValue:forUndefinedKey:Invoked by setValue:forKey: when it finds no property - ()setValue:()value forUndefinedKey:(NSString *DiscussionSubclasses can method to handle the request some other way. The implementation raises an NSUndefinedKeyException.
When the key parameter of the setValue method cannot find the corresponding member in the class, this method is called. rewriting it can prevent the NSUndefinedKeyException from being thrown.
In this case, the CAKeyFrameAnimation class (or its parent class) should overwrite setValue: forUndefinedKey:, but how is it processed in the method so that valueForKey can get the value correctly?
The Extension Mechanism of objective-c has two features: category and associative. Category extension category and associative extension attribute. To use associative, You need to import the header file <objc/runtime. h>.
Use category to extend the NSObject category and use associative and setValue: forUndefinedKey: To enable NSObject to store any key-value pair.
<objc/runtime.h>- ()valueForUndefinedKey:(NSString *- ()setValue:()value forUndefinedKey:(NSString *- ()valueForUndefinedKey:(NSString *- ()setValue:()value forUndefinedKey:(NSString * ([value isKindOfClass:[NSString
Try again now:
NSObject *obj = forKeyPath:,[obj valueForKey:]);
You can output it.