CALayer and CAAnimation's dynamic resolution of unimplemented property accessors

£可爱£侵袭症+ 提交于 2019-12-11 03:28:00

问题


I found that CALayer and CAAnimation don't only extend the NSKeyValueCoding protocol as described in Core Animation Extensions To Key-Value Coding, but also offer dynamic resolution for unimplemented property accessors. For example:

@interface DotLayer : CALayer
@property (nonatomic, retain) id dot;
@end

@implementation DotLayer
@dynamic dot;
@end

Simply with a property declaration and stating it is @dynamic, I can access dot property without implementing its accessors:

DotLayer *layer = [DotLayer layer];
NSLog(@"layer responds to dot: %d", [layer respondsToSelector:@selector(dot)]);
layer.dot = nil;
NSLog(@"%@", [layer dot]);

After further investigation, I found this dynamic resolution is done by CALayer and CAAnimation's special implementation of +resolveInstanceMethod:.

I saw usage of this dynamic resolution in ImageBrowser sample code of WWDC 2010, but I can not find any documentation stating this feature. So I'm wondering:
Is this dynamic resolution a prescribed behavior that I can make use of in my own code?


回答1:


After further thought, I have my own guess: To extend the NSKeyValueCoding protocol, CALayer and CAAnimation offer dynamic resolution for all unimplemented property accessors in their implementation of +resolveInstanceMethod:. As a side effect, @dynamic properties without accessors are also covered.

I'm not sure whether the WWDC sample code is written this way intentionally or accidentally. But if my guess is correct, it is quite implementation dependent so I think we should not utilize it as a feature.



来源:https://stackoverflow.com/questions/6516322/calayer-and-caanimations-dynamic-resolution-of-unimplemented-property-accessors

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!